﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using C1.Win.C1Input;
using PCSComProduct.Items.BO;
using PCSComUtils.Common;
using PCSComUtils.Common.BO;
using PCSComUtils.DataContext;
using PCSComUtils.MasterSetup.BO;
using PCSComUtils.PCSExc;
using PCSProduct.STDCost;
using PCSUtils.Framework.ReportFrame;
using PCSUtils.Log;
using PCSUtils.Utils;

namespace PCSProduct.Items
{
    /// <summary>
    /// Summary description for ProductItemInfo.
    /// </summary>
    public partial class ProductItemInfo : Form
    {
        #region Define variable for this form

        private const string This = "PCSProduct.Items.ProductItemInfo";
        private const string ZeroString = "0";
        private const string VCustomerVendor = "V_VendorCustomer";
        private const string DecimalNumberformatSmall = "##############,0.0000";
        private int _copiedProductId;
        private bool _dataIsValid;
        private EnumAction _enumAction;
        private bool _firstLoad;
        private int _intProductId;
        private Bitmap _mPicture;
        private int? _producttionLineId;
        private string _strPathImage = "";
        private ITM_Product _voProduct;

        #endregion

        public ProductItemInfo()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCost_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnCost_Click()";
            try
            {
                if (_intProductId > 0)
                {
                    var frmItemStandardCost = new ItemStandardCost(_intProductId);
                    frmItemStandardCost.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void SetEditMask()
        {
            //Quantity Set
            numQuantitySet.FormatType = FormatTypeEnum.CustomFormat;
            numQuantitySet.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //List Price
            numPurchasingPrice.FormatType = FormatTypeEnum.CustomFormat;
            const string DECIMAL_FORMAT = "##############,0.0000";
            numPurchasingPrice.CustomFormat = DECIMAL_FORMAT;

            //License Fee
            numLicenseFee.FormatType = FormatTypeEnum.CustomFormat;
            numLicenseFee.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Length 
            txtLength.FormatType = FormatTypeEnum.CustomFormat;
            txtLength.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Rate 
            txtRate.FormatType = FormatTypeEnum.CustomFormat;
            txtRate.CustomFormat = DecimalNumberformatSmall;

            //Height
            txtHeight.FormatType = FormatTypeEnum.CustomFormat;
            txtHeight.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Width
            txtWidth.FormatType = FormatTypeEnum.CustomFormat;
            txtWidth.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Weight
            txtWeight.FormatType = FormatTypeEnum.CustomFormat;
            txtWeight.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Shelife
            txtShelfLife.FormatType = FormatTypeEnum.CustomFormat;
            txtShelfLife.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Lot Size
            txtLotSize.FormatType = FormatTypeEnum.CustomFormat;
            txtLotSize.CustomFormat = Constants.INTERGER_NUMBERFORMAT;

            //Safety stock
            txtSafetyStock.FormatType = FormatTypeEnum.CustomFormat;
            txtSafetyStock.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Scrap percent
            txtScrapPercent.FormatType = FormatTypeEnum.CustomFormat;
            txtScrapPercent.CustomFormat = DecimalNumberformatSmall;

            //Max stock
            txtMaximumStock.FormatType = FormatTypeEnum.CustomFormat;
            txtMaximumStock.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Min Stock
            txtMinimumStock.FormatType = FormatTypeEnum.CustomFormat;
            txtMinimumStock.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //issue size
            txtIssueSize.FormatType = FormatTypeEnum.CustomFormat;
            txtIssueSize.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;


            //Voucher tolerance
            txtVoucherTolerance.FormatType = FormatTypeEnum.CustomFormat;
            txtVoucherTolerance.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Conversion tolerance
            txtConversionTolerance.FormatType = FormatTypeEnum.CustomFormat;
            txtConversionTolerance.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Lead Time Fixed
            txtLTFixedTime.FormatType = FormatTypeEnum.CustomFormat;
            txtLTFixedTime.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Lead Time Safety stock
            txtLTSafetyStock.FormatType = FormatTypeEnum.CustomFormat;
            txtLTSafetyStock.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Lead Time variable
            txtLTVariableTime.FormatType = FormatTypeEnum.CustomFormat;
            txtLTVariableTime.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Lead Time Order Prepare
            txtLTOrderPrepare.FormatType = FormatTypeEnum.CustomFormat;
            txtLTOrderPrepare.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Lead Time Dock To Stock
            txtLTDockToStock.FormatType = FormatTypeEnum.CustomFormat;
            txtLTDockToStock.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Lead Time Sale ATP
            txtLTSalesATP.FormatType = FormatTypeEnum.CustomFormat;
            txtLTSalesATP.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Lead Time Shipping Prepare
            txtLTShippingPrepare.FormatType = FormatTypeEnum.CustomFormat;
            txtLTShippingPrepare.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Lead Time Requisition
            txtLTRequisition.FormatType = FormatTypeEnum.CustomFormat;
            txtLTRequisition.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //VAT Tax
            txtVAT.FormatType = FormatTypeEnum.CustomFormat;
            txtVAT.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Import Tax
            txtImportTax.FormatType = FormatTypeEnum.CustomFormat;
            txtImportTax.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Export Tax
            txtExportTax.FormatType = FormatTypeEnum.CustomFormat;
            txtExportTax.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Special Tax
            txtSpecialTax.FormatType = FormatTypeEnum.CustomFormat;
            txtSpecialTax.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;

            //Order
            txtOrderQuantity.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;
            txtOrderQuantityMultiple.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;
        }

        private void ProductItemInfo_Load(object sender, EventArgs e)
        {
            _firstLoad = true;
            const string METHOD_NAME = This + ".ProductItemInfo_Load()";
            try
            {
                //Set authorization for user
                var secForm = new Security();
                Name = This;
                if (secForm.SetRightForUserOnForm(this, SystemProperty.UserName) == 0)
                {
                    // You don't have the right to view this item
                    PCSMessageBox.Show(ErrorCode.MESSAGE_YOU_HAVE_NO_RIGHT_TO_VIEW, MessageBoxIcon.Warning);
                    Close();
                    return;
                }

                _enumAction = new EnumAction();
                _enumAction = EnumAction.Default;
                _intProductId = -1; //No product at the load time

                LoadDataForCombo();

                //clear all controls value on form
                ClearForm();

                //lock all controls
                LockForm(true);

                //Diable or Enable buttons
                EnableDisableButtons();

                //set the display and edit mask for C1numberEdit
                SetEditMask();

                //focus on the first tab
                tabProductInfo.SelectedIndex = 0;
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void LoadDataForCombo()
        {
            var objProductItemInfoBO = new ProductItemInfoBO();
            var dtTmp = new DataTable();

            //Load data for CNN combo box

            FormControlComponents.PutDataIntoC1ComboBox(cboCCN, objProductItemInfoBO.GetCCN(), MST_CCNTable.CODE_FLD,
                                                        MST_CCNTable.CCNID_FLD, MST_CCNTable.TABLE_NAME);

            //Load Stock Unit of Measure
            dtTmp = objProductItemInfoBO.GetUnitOfMeasure();
            FormControlComponents.PutDataIntoC1ComboBox(cboStockUMID, dtTmp.Copy(), MST_UnitOfMeasureTable.CODE_FLD,
                                                        MST_UnitOfMeasureTable.UNITOFMEASUREID_FLD,
                                                        MST_UnitOfMeasureTable.TABLE_NAME);

            //Buying UNT of Measure
            FormControlComponents.PutDataIntoC1ComboBox(cboBuyingUMID, dtTmp.Copy(), MST_UnitOfMeasureTable.CODE_FLD,
                                                        MST_UnitOfMeasureTable.UNITOFMEASUREID_FLD,
                                                        MST_UnitOfMeasureTable.TABLE_NAME);

            //Selling Unit Of measure
            FormControlComponents.PutDataIntoC1ComboBox(cboSellingUMID, dtTmp.Copy(), MST_UnitOfMeasureTable.CODE_FLD,
                                                        MST_UnitOfMeasureTable.UNITOFMEASUREID_FLD,
                                                        MST_UnitOfMeasureTable.TABLE_NAME);

            //Length Unit Of measure
            FormControlComponents.PutDataIntoC1ComboBox(cboLengthUMID, dtTmp.Copy(), MST_UnitOfMeasureTable.CODE_FLD,
                                                        MST_UnitOfMeasureTable.UNITOFMEASUREID_FLD,
                                                        MST_UnitOfMeasureTable.TABLE_NAME);

            //Width Unit of measure
            FormControlComponents.PutDataIntoC1ComboBox(cboWidthUMID, dtTmp.Copy(), MST_UnitOfMeasureTable.CODE_FLD,
                                                        MST_UnitOfMeasureTable.UNITOFMEASUREID_FLD,
                                                        MST_UnitOfMeasureTable.TABLE_NAME);

            //Height Unit of measure
            FormControlComponents.PutDataIntoC1ComboBox(cboWidthUMID, dtTmp.Copy(), MST_UnitOfMeasureTable.CODE_FLD,
                                                        MST_UnitOfMeasureTable.UNITOFMEASUREID_FLD,
                                                        MST_UnitOfMeasureTable.TABLE_NAME);

            //Height unit of measure
            FormControlComponents.PutDataIntoC1ComboBox(cboHeightUMID, dtTmp.Copy(), MST_UnitOfMeasureTable.CODE_FLD,
                                                        MST_UnitOfMeasureTable.UNITOFMEASUREID_FLD,
                                                        MST_UnitOfMeasureTable.TABLE_NAME);

            //Weight unit of measure
            FormControlComponents.PutDataIntoC1ComboBox(cboWeightUMID, dtTmp.Copy(), MST_UnitOfMeasureTable.CODE_FLD,
                                                        MST_UnitOfMeasureTable.UNITOFMEASUREID_FLD,
                                                        MST_UnitOfMeasureTable.TABLE_NAME);

            //Design unit of measure
            FormControlComponents.PutDataIntoC1ComboBox(cboDesignUMID, dtTmp.Copy(), MST_UnitOfMeasureTable.CODE_FLD,
                                                        MST_UnitOfMeasureTable.UNITOFMEASUREID_FLD,
                                                        MST_UnitOfMeasureTable.TABLE_NAME);

            //HACK: modify by Tuan TQ. Bind Cost method from database				
            FormControlComponents.PutDataIntoC1ComboBox(cboCostMethod, objProductItemInfoBO.GetCostMethod().Copy(),
                                                        enm_CostMethodTable.DESCRIPTION_FLD,
                                                        enm_CostMethodTable.CODE_FLD, enm_CostMethodTable.TABLE_NAME);
            //End hack

            //Account Grouping Code
            FormControlComponents.PutDataIntoC1ComboBox(cboAccountCode, objProductItemInfoBO.GetAGC(),
                                                        MST_AGCTable.CODE_FLD, MST_AGCTable.AGCID_FLD,
                                                        MST_AGCTable.TABLE_NAME);

            //Product Type
            cboProductType.DataSource = objProductItemInfoBO.GetProductType();
            cboProductType.DisplayMember = ITM_ProductTypeTable.CODE_FLD;
            cboProductType.ValueMember = ITM_ProductTypeTable.PRODUCTTYPEID_FLD;

            //Master Location, Bin, and Location
            DataSet dstLocation = objProductItemInfoBO.GetLocation();
            FormControlComponents.PutDataIntoC1ComboBox(cboMasterLocationID,
                                                        dstLocation.Tables[MST_MasterLocationTable.TABLE_NAME],
                                                        MST_MasterLocationTable.CODE_FLD,
                                                        MST_MasterLocationTable.MASTERLOCATIONID_FLD,
                                                        MST_MasterLocationTable.TABLE_NAME);
            FormControlComponents.PutDataIntoC1ComboBox(cboLocationID, dstLocation.Tables[MST_LocationTable.TABLE_NAME],
                                                        MST_LocationTable.CODE_FLD, MST_LocationTable.LOCATIONID_FLD,
                                                        MST_LocationTable.TABLE_NAME);
            FormControlComponents.PutDataIntoC1ComboBox(cboBinID, dstLocation.Tables[MST_BINTable.TABLE_NAME],
                                                        MST_BINTable.CODE_FLD, MST_BINTable.BINID_FLD,
                                                        MST_BINTable.TABLE_NAME);

            //Category
            FormControlComponents.PutDataIntoC1ComboBox(cboCategoryID, objProductItemInfoBO.GetCategory(),
                                                        ITM_CategoryTable.CODE_FLD, ITM_CategoryTable.CATEGORYID_FLD,
                                                        ITM_CategoryTable.TABLE_NAME);

            //Source combo box
            FormControlComponents.PutDataIntoC1ComboBox(cboSourceID, objProductItemInfoBO.GetSource(),
                                                        ITM_SourceTable.CODE_FLD, ITM_SourceTable.SOURCEID_FLD,
                                                        ITM_SourceTable.TABLE_NAME);

            //Harzard
            FormControlComponents.PutDataIntoC1ComboBox(cboHazardID, objProductItemInfoBO.GetHarzard(),
                                                        ITM_HazardTable.CODE_FLD, ITM_HazardTable.HAZARDID_FLD,
                                                        ITM_HazardTable.TABLE_NAME);

            //Freight Class
            FormControlComponents.PutDataIntoC1ComboBox(cboFreightClassID, objProductItemInfoBO.GetFreightClass(),
                                                        ITM_FreightClassTable.CODE_FLD,
                                                        ITM_FreightClassTable.FREIGHTCLASSID_FLD,
                                                        ITM_FreightClassTable.TABLE_NAME);
            //Deleate Reason
            FormControlComponents.PutDataIntoC1ComboBox(cboReasonID, objProductItemInfoBO.GetDeleteReason(),
                                                        ITM_DeleteReasonTable.CODE_FLD,
                                                        ITM_DeleteReasonTable.DELETEREASONID_FLD,
                                                        ITM_DeleteReasonTable.TABLE_NAME);
            //Format code
            FormControlComponents.PutDataIntoC1ComboBox(cboFormatCodeID, objProductItemInfoBO.GetFormatCodes(),
                                                        ITM_FormatCodeTable.CODE_FLD,
                                                        ITM_FormatCodeTable.FORMATCODEID_FLD,
                                                        ITM_FormatCodeTable.TABLE_NAME);
            //Delivery policy

            FormControlComponents.PutDataIntoC1ComboBox(cboDeliveryPolicyID, objProductItemInfoBO.GetDeliveryPolicy(),
                                                        ITM_DeliveryPolicyTable.CODE_FLD,
                                                        ITM_DeliveryPolicyTable.DELIVERYPOLICYID_FLD,
                                                        ITM_DeliveryPolicyTable.TABLE_NAME);
            //Order Policy
            FormControlComponents.PutDataIntoC1ComboBox(cboOrderPolicyID, objProductItemInfoBO.GetOrderPolicy(),
                                                        ITM_OrderPolicyTable.CODE_FLD,
                                                        ITM_OrderPolicyTable.ORDERPOLICYID_FLD,
                                                        ITM_OrderPolicyTable.TABLE_NAME);
            //Ship Tolerence
            FormControlComponents.PutDataIntoC1ComboBox(cboShipToleranceID, objProductItemInfoBO.GetShipTolerence(),
                                                        ITM_ShipToleranceTable.CODE_FLD,
                                                        ITM_ShipToleranceTable.SHIPTOLERANCEID_FLD,
                                                        ITM_ShipToleranceTable.TABLE_NAME);
            //Buyer combo box
            FormControlComponents.PutDataIntoC1ComboBox(cboBuyerID, objProductItemInfoBO.GetBuyer(),
                                                        ITM_BuyerTable.CODE_FLD, ITM_BuyerTable.BUYERID_FLD,
                                                        ITM_BuyerTable.TABLE_NAME);
            //Vendor Location
            txtPrimaryVendorID.Text = string.Empty;
            FormControlComponents.PutDataIntoC1ComboBox(cboVendorLocationID, objProductItemInfoBO.GetVendorLocation(),
                                                        MST_PartyLocationTable.CODE_FLD,
                                                        MST_PartyLocationTable.PARTYLOCATIONID_FLD,
                                                        MST_PartyLocationTable.TABLE_NAME);
            ChangePrimaryVendorID();

            //Order rule
            FormControlComponents.PutDataIntoC1ComboBox(cboOrderRuleID, objProductItemInfoBO.GetOrderRule(),
                                                        ITM_OrderPolicyTable.CODE_FLD,
                                                        ITM_OrderRuleTable.ORDERRULEID_FLD,
                                                        ITM_OrderRuleTable.TABLE_NAME);
        }

        private void LoadProductInfor(int productId)
        {
            const string SEPARATE_STRING = "##";

            int intPreviousTabIndex = tabProductInfo.SelectedIndex;

            var objProductItemInfoBO = new ProductItemInfoBO();
            _voProduct = objProductItemInfoBO.GetProductInfo(productId);

            //Update this value object on the screen
            txtCode.Text = _voProduct.Code;
            _producttionLineId = _voProduct.ProductionLineID;
            txtDescription.Text = _voProduct.Description;
            txtRevision.Text = _voProduct.Revision;
            txtStockTakingCode.Text = _voProduct.StockTakingCode;
            txtSetUpPair.Text = _voProduct.SetUpPair;

            txtRegisteredCode.Text = _voProduct.RegisteredCode;

            //display CCN combo
            if (_voProduct.CCNID > 0)
                cboCCN.SelectedValue = _voProduct.CCNID;
            else
                cboCCN.SelectedIndex = -1;
            //Make Item
            chkMakeItem.Checked = _voProduct.MakeItem;
            //Category 
            if (_voProduct.CategoryID > 0)
                cboCategoryID.SelectedValue = _voProduct.CategoryID;
            else
                cboCategoryID.SelectedIndex = -1;
            //Lot Control
            chkLotControl.Checked = _voProduct.LotControl ?? false;
            //Stock Unit Of measure
            if (_voProduct.StockUMID > 0)
                cboStockUMID.SelectedValue = _voProduct.StockUMID;
            else
                cboStockUMID.SelectedIndex = -1;

            //Stock Unit Of measure
            if (_voProduct.DesignUMID > 0)
                cboDesignUMID.SelectedValue = _voProduct.DesignUMID;
            else
                cboDesignUMID.SelectedIndex = -1;

            //Buying Unit of measure
            if (_voProduct.BuyingUMID > 0)
                cboBuyingUMID.SelectedValue = _voProduct.BuyingUMID;
            else
                cboBuyingUMID.SelectedIndex = -1;
            //Selling Unit of measure
            if (_voProduct.SellingUMID > 0)
                cboSellingUMID.SelectedValue = _voProduct.SellingUMID;
            else
                cboSellingUMID.SelectedIndex = -1;
            //Cost method
            if (_voProduct.CostMethod >= 0)
                cboCostMethod.SelectedValue = _voProduct.CostMethod;
            else
                cboCostMethod.SelectedIndex = -1;
            //AGC
            if (_voProduct.AGCID > 0)
                cboAccountCode.SelectedValue = _voProduct.AGCID;
            else
                cboAccountCode.SelectedIndex = -1;
            //QA status
            chkQAStatus.Checked = _voProduct.QAStatus > 0;
            //Master Location
            if (_voProduct.MasterLocationID != null && _voProduct.MasterLocationID > 0)
                cboMasterLocationID.SelectedValue = _voProduct.MasterLocationID;
            else
                cboMasterLocationID.SelectedIndex = -1;
            //Location
            if (_voProduct.LocationID != null && _voProduct.LocationID > 0)
                cboLocationID.SelectedValue = _voProduct.LocationID;
            else
                cboLocationID.SelectedIndex = -1;
            //Bin
            if (_voProduct.BinID > 0)
                cboBinID.SelectedValue = _voProduct.BinID;
            else
                cboBinID.SelectedIndex = -1;
            //Work Center
            if (_voProduct.WorkCenterID != null && _voProduct.WorkCenterID > 0)
            {
                txtWorkcenter.Tag = _voProduct.WorkCenterID;
                txtWorkcenter.Text = _voProduct.MST_WorkCenter.Code;
            }
            else
            {
                txtWorkcenter.Tag = null;
                txtWorkcenter.Text = string.Empty;
            }
            //Shelf Life
            if (_voProduct.ShelfLife != null)
                txtShelfLife.Value = _voProduct.ShelfLife;
            else
                txtShelfLife.Value = DBNull.Value;
            //Source 
            if (_voProduct.SourceID > 0)
                cboSourceID.SelectedValue = _voProduct.SourceID;
            else
                cboSourceID.SelectedIndex = -1;
            //VAT
            if (_voProduct.VAT != null)
                txtVAT.Value = _voProduct.VAT;
            else
                txtVAT.Value = DBNull.Value;
            //Import Tax
            if (_voProduct.ImportTax != null)
                txtImportTax.Value = _voProduct.ImportTax;
            else
                txtImportTax.Value = DBNull.Value;
            //Export Tax
            if (_voProduct.ExportTax != null)
                txtExportTax.Value = _voProduct.ExportTax;
            else
                txtExportTax.Value = DBNull.Value;
            //Special Tax
            if (_voProduct.SpecialTax != null)
                txtSpecialTax.Value = _voProduct.SpecialTax;
            else
                txtSpecialTax.Value = DBNull.Value;
            //Hazard 
            if (_voProduct.HazardID != null && _voProduct.HazardID > 0)
                cboHazardID.SelectedValue = _voProduct.HazardID;
            else
                cboHazardID.SelectedIndex = -1;
            //Freight Class 
            if (_voProduct.FreightClassID != null && _voProduct.FreightClassID > 0)
                cboFreightClassID.SelectedValue = _voProduct.FreightClassID;
            else
                cboFreightClassID.SelectedIndex = -1;
            //Delete Reason 
            if (_voProduct.DeleteReasonID != null && _voProduct.DeleteReasonID > 0)
                cboReasonID.SelectedValue = _voProduct.DeleteReasonID;
            else
                cboReasonID.SelectedIndex = -1;
            //Stock
            chkStock.Checked = _voProduct.Stock ?? false;
            //Lot Size
            if (_voProduct.LotSize != null)
                txtLotSize.Value = _voProduct.LotSize;
            else
                txtLotSize.Value = DBNull.Value;
            //Format code
            if (_voProduct.FormatCodeID != null)
                cboFormatCodeID.SelectedValue = _voProduct.FormatCodeID;
            txtPartNumber.Value = _voProduct.PartNumber;
            txtPartNumber.ReadOnly = true;
            //Other infor 1
            txtOtherInfo1.Text = _voProduct.OtherInfo1;
            //Other infor 2
            txtOtherInfo2.Text = _voProduct.OtherInfo2;
            //Length
            if (_voProduct.Length != null)
                txtLength.Value = _voProduct.Length;
            else
                txtLength.Value = DBNull.Value;

            //Length
            if (_voProduct.Rate != null && _voProduct.Rate > 0)
                txtRate.Value = _voProduct.Rate;
            else
                txtRate.Value = DBNull.Value;

            if (_voProduct.LengthUMID != null && _voProduct.LengthUMID > 0)
                cboLengthUMID.SelectedValue = _voProduct.LengthUMID;
            else
                cboLengthUMID.SelectedIndex = -1;
            //Width 
            if (_voProduct.Width != null)
                txtWidth.Value = _voProduct.Width;
            else
                txtWidth.Value = DBNull.Value;
            if (_voProduct.WidthUMID != null)
                cboWidthUMID.SelectedValue = _voProduct.WidthUMID;
            else
                cboWidthUMID.SelectedIndex = -1;
            //Height 
            if (_voProduct.Height != null)
                txtHeight.Value = _voProduct.Height;
            else
                txtHeight.Value = DBNull.Value;
            if (_voProduct.HeightUMID != null && _voProduct.HeightUMID > 0)
                cboHeightUMID.SelectedValue = _voProduct.HeightUMID;
            else
                cboHeightUMID.SelectedIndex = -1;
            //Weight
            if (_voProduct.Weight != null)
                txtWeight.Value = _voProduct.Weight;
            else
                txtWeight.Value = DBNull.Value;
            if (_voProduct.WeightUMID != null)
                cboWeightUMID.SelectedValue = _voProduct.WeightUMID;
            else
                cboWeightUMID.SelectedIndex = -1;
            //Setup Date
            dtmSetupDate.Value = _voProduct.SetupDate;
            //Plan Type
            if (_voProduct.PlanType == (int) PlanTypeEnum.MPS)
                radPlanTypeMPS.Checked = true;
            if (_voProduct.PlanType == (int) PlanTypeEnum.MRP)
                radPlanTypeMRP.Checked = true;
            //Delivery policy
            if (_voProduct.DeliveryPolicyID != null && _voProduct.DeliveryPolicyID > 0)
                cboDeliveryPolicyID.SelectedValue = _voProduct.DeliveryPolicyID;
            else
                cboDeliveryPolicyID.SelectedIndex = -1;
            //Order Policy
            if (_voProduct.OrderPolicyID != null && _voProduct.OrderPolicyID > 0)
                cboOrderPolicyID.SelectedValue = _voProduct.OrderPolicyID;
            else
                cboOrderPolicyID.SelectedIndex = -1;
            //Ship Tolerence 
            if (_voProduct.ShipToleranceID != null && _voProduct.ShipToleranceID > 0)
                cboShipToleranceID.SelectedValue = _voProduct.ShipToleranceID;
            else
                cboShipToleranceID.SelectedIndex = -1;
            //Auto Conversion 
            chkAutoConversion.Checked = _voProduct.AutoConversion ?? false;
            //Buyer
            if (_voProduct.BuyerID != null && _voProduct.BuyerID > 0)
                cboBuyerID.SelectedValue = _voProduct.BuyerID;
            else
                cboBuyerID.SelectedIndex = -1;
            if (_voProduct.PrimaryVendorID != null && _voProduct.PrimaryVendorID > 0)
            {
                int iPrimaryVendorId = _voProduct.PrimaryVendorID ?? 0;
                string strPartyCodeAndName = objProductItemInfoBO.GetVendorCodeAndName(iPrimaryVendorId);
                int iIndex = strPartyCodeAndName.IndexOf(SEPARATE_STRING);

                txtPrimaryVendor.Text = strPartyCodeAndName.Substring(0, iIndex);
                txtVendorName.Text = strPartyCodeAndName.Substring(iIndex + 2);
                txtPrimaryVendorID.Text = _voProduct.PrimaryVendorID.ToString();
            }
            else
            {
                txtPrimaryVendorID.Text = string.Empty;
                txtPrimaryVendor.Text = string.Empty;
                txtVendorName.Text = string.Empty;
            }
            ChangePrimaryVendorID();
            //Vendor Location
            if (_voProduct.VendorLocationID != null && _voProduct.VendorLocationID > 0)
                cboVendorLocationID.SelectedValue = _voProduct.VendorLocationID;
            else
                cboVendorLocationID.SelectedIndex = -1;
            //Order Rule
            if (_voProduct.OrderRuleID != null && _voProduct.OrderRuleID > 0)
                cboOrderRuleID.SelectedValue = _voProduct.OrderRuleID;
            else
                cboOrderRuleID.SelectedIndex = -1;
            //Min Oder Quantity
            if (_voProduct.OrderQuantity != null)
                txtOrderQuantity.Value = _voProduct.OrderQuantity;
            else
                txtOrderQuantity.Value = DBNull.Value;
            //Order Multiple Quantity
            if (_voProduct.OrderQuantityMultiple != null)
                txtOrderQuantityMultiple.Value = _voProduct.OrderQuantityMultiple;
            else
                txtOrderQuantityMultiple.Value = DBNull.Value;
            //Order point
            if (_voProduct.OrderPoint != null)
                txtOrderPoint.Value = _voProduct.OrderPoint;
            else
                txtOrderPoint.Value = DBNull.Value;
            //Safety stock
            if (_voProduct.SafetyStock != null)
                txtSafetyStock.Value = _voProduct.SafetyStock;
            else
                txtSafetyStock.Value = DBNull.Value;
            //Scrapt Percent
            if (_voProduct.ScrapPercent != null)
                txtScrapPercent.Value = _voProduct.ScrapPercent;
            else
                txtScrapPercent.Value = DBNull.Value;
            //Maximum Stock
            if (_voProduct.MaximumStock != null)
                txtMaximumStock.Value = _voProduct.MaximumStock;
            else
                txtMaximumStock.Value = DBNull.Value;
            //Minimum stock
            if (_voProduct.MinimumStock != null)
                txtMinimumStock.Value = _voProduct.MinimumStock;
            else
                txtMinimumStock.Value = DBNull.Value;
            //Issue Size
            if (_voProduct.IssueSize != null)
                txtIssueSize.Value = _voProduct.IssueSize;
            else
                txtIssueSize.Value = DBNull.Value;
            //Voucher tolerence
            if (_voProduct.VoucherTolerance != null)
                txtVoucherTolerance.Value = _voProduct.VoucherTolerance;
            else
                txtVoucherTolerance.Value = DBNull.Value;
            //Conversion Tolerence
            if (_voProduct.ConversionTolerance != null)
                txtConversionTolerance.Value = _voProduct.ConversionTolerance;
            else
                txtConversionTolerance.Value = DBNull.Value;
            //Lead Time Fixed Time
            if (_voProduct.LTFixedTime != null)
                txtLTFixedTime.Value = _voProduct.LTFixedTime;
            else
                txtLTFixedTime.Value = DBNull.Value;
            //LeadTime Safety Stock
            if (_voProduct.LTSafetyStock != null)
                txtLTSafetyStock.Value = _voProduct.LTSafetyStock;
            else
                txtLTSafetyStock.Value = DBNull.Value;
            //LTVariable
            if (_voProduct.LTVariableTime != null)
                txtLTVariableTime.Value = _voProduct.LTVariableTime;
            else
                txtLTVariableTime.Value = DBNull.Value;
            //LT Order Prepare
            if (_voProduct.LTOrderPrepare != null)
                txtLTOrderPrepare.Value = _voProduct.LTOrderPrepare;
            else
                txtLTOrderPrepare.Value = DBNull.Value;
            //LTDocToStock
            if (_voProduct.LTDockToStock != null)
                txtLTDockToStock.Value = _voProduct.LTDockToStock;
            else
                txtLTDockToStock.Value = DBNull.Value;
            //LTSales ATP
            if (_voProduct.LTSalesATP != null)
                txtLTSalesATP.Value = _voProduct.LTSalesATP;
            else
                txtLTSalesATP.Value = DBNull.Value;
            //LTShip Prepare
            if (_voProduct.LTShippingPrepare != null)
                txtLTShippingPrepare.Value = _voProduct.LTShippingPrepare;
            else
                txtLTShippingPrepare.Value = DBNull.Value;
            //LT Requisition
            if (_voProduct.LTRequisition != null)
                txtLTRequisition.Value = _voProduct.LTRequisition;
            else
                txtLTRequisition.Value = DBNull.Value;
            //HACKED: Added by Tuan TQ -- 2005-09-22 
            txtPartNameVN.Text = _voProduct.PartNameVN;
            if (_voProduct.ProductTypeID != null)
                cboProductType.SelectedValue = _voProduct.ProductTypeID;
            txtTaxCode.Text = _voProduct.TaxCode;
            //LicenseFee
            if (_voProduct.LicenseFee != null)
                numLicenseFee.Value = _voProduct.LicenseFee;
            else
                numLicenseFee.Value = DBNull.Value;
            //ListPrice
            if (_voProduct.ListPrice != null)
                numPurchasingPrice.Value = _voProduct.ListPrice;
            else
                numPurchasingPrice.Value = DBNull.Value;
            //Min Produce
            if (_voProduct.MinProduce != null)
                numMinProduce.Value = _voProduct.MinProduce;
            else
                numMinProduce.Value = DBNull.Value;
            //Max Produce
            if (_voProduct.MaxProduce != null)
                numMaxProduce.Value = _voProduct.MaxProduce;
            else
                numMaxProduce.Value = DBNull.Value;
            //QuantitySet
            if (_voProduct.QuantitySet != null)
                numQuantitySet.Value = _voProduct.QuantitySet;
            else
                numQuantitySet.Value = DBNull.Value;

            //Inventor
            if (_voProduct.InventorID != null)
            {
                txtInventor.Tag = _voProduct.InventorID;
                int iInventorID = _voProduct.InventorID ?? 0;
                txtInventor.Text = new PartyBO().GetParty(iInventorID, string.Empty).Code;
            }
            else
            {
                txtInventor.Text = string.Empty;
                txtInventor.Tag = null;
            }

            if (_voProduct.VendorCurrencyID != null)
            {
                int iVendorCurrencyID = _voProduct.VendorCurrencyID ?? 0;
                txtCurrency.Tag = _voProduct.VendorCurrencyID;
                txtCurrency.Text = objProductItemInfoBO.GetCurrencyCode(iVendorCurrencyID);
            }
            else
            {
                txtCurrency.Tag = null;
                txtCurrency.Text = string.Empty;
            }

            //HACK: added by Tuan TQ. 17 May, 2006
            //MaxRoundUpToMin
            if (_voProduct.MaxRoundUpToMin != null)
            {
                numRoundUpToMin.Value = _voProduct.MaxRoundUpToMin;
            }
            else
            {
                numRoundUpToMin.Value = DBNull.Value;
            }

            //MaxRoundUpToMultiple
            if (_voProduct.MaxRoundUpToMultiple != null)
            {
                numRoundUpToMultiple.Value = _voProduct.MaxRoundUpToMultiple;
            }
            else
            {
                numRoundUpToMultiple.Value = DBNull.Value;
            }

            //AC Adjustment Code			
            if (_voProduct.ACAdjustmentMasterID != null)
            {
                txtACAdjustment.Tag = _voProduct.ACAdjustmentMasterID;
                txtACAdjustment.Text = _voProduct.cst_ACAdjustmentMaster.Code;
            }
            else
            {
                txtACAdjustment.Tag = null;
                txtACAdjustment.Text = string.Empty;
            }
            if (_voProduct.Picture != null)
            {
                byte[] content = _voProduct.Picture.ToArray();
                var stream = new MemoryStream(content);
                _mPicture = new Bitmap(stream);
            }
            // aveg
            chkAVEG.Checked = _voProduct.AVEG ?? false;
            // mass order
            chkMassOrder.Checked = _voProduct.MassOrder ?? false;
            //End_ Added by Tuan TQ -- 2005-09-22
            // group info
            txtGroup1.Text = _voProduct.Group1;
            txtGroup2.Text = _voProduct.Group2;
            txtGroupQuantity.Value = _voProduct.GroupQuantity;
            //restore to the previous tab index
            tabProductInfo.SelectedIndex = intPreviousTabIndex;
            //Change to Default
            _enumAction = EnumAction.Default;
            //Enable and Disable Buttons
            EnableDisableButtons();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnAdd_Click()";
            var objUtilsBO = new UtilsBO();
            try
            {
                //Turn to Add action
                _enumAction = EnumAction.Add;

                //Unlock form
                LockForm(false);

                //clear controls
                ClearForm();

                //
                if (SystemProperty.CCNID > 0)
                {
                    cboCCN.SelectedValue = SystemProperty.CCNID;
                }

                //setup date
                dtmSetupDate.Value = objUtilsBO.GetDBDate();
                //Fill Default Master Location 
                cboMasterLocationID.SelectedText = SystemProperty.MasterLocationCode;
                cboMasterLocationID.SelectedValue = SystemProperty.MasterLocationID;
                //Enable Button
                EnableDisableButtons();

                //Don't allow to search for existing product
                radPlanTypeMRP.Checked = true;
                // default checked
                chkAVEG.Checked = true;
                // default false
                chkMassOrder.Checked = false;

                txtCode.Focus();
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void EnableDisableButtons()
        {
            switch (_enumAction)
            {
                case EnumAction.Add:
                    //Disable Buttons
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnCopy.Enabled = false;
                    btnBOM.Enabled = false;
                    btnRouting.Enabled = false;
                    btnCost.Enabled = false;
                    btnSearchProductCode.Enabled = false;
                    btnSearchProductDescription.Enabled = false;
                    btnTakingCode.Enabled = false;

                    //Enable Buttons
                    btnSave.Enabled = true;
                    btnworkcenter.Enabled = true;
                    btnSearchVendor.Enabled = true;

                    break;
                case EnumAction.Edit:
                    //Disable Buttons
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnCopy.Enabled = false;
                    btnBOM.Enabled = false;
                    btnRouting.Enabled = false;
                    btnCost.Enabled = false;
                    btnSearchProductCode.Enabled = false;
                    btnSearchProductDescription.Enabled = false;
                    btnTakingCode.Enabled = false;

                    //Enable Buttons
                    btnSave.Enabled = true;
                    btnworkcenter.Enabled = true;
                    btnSearchVendor.Enabled = true;
                    break;
                case EnumAction.Default:
                    //Disable Buttons
                    btnSave.Enabled = false;
                    btnSearchVendor.Enabled = false;

                    btnAdd.Enabled = true;
                    btnSearchProductCode.Enabled = true;
                    btnSearchProductDescription.Enabled = true;
                    btnTakingCode.Enabled = true;
                    btnworkcenter.Enabled = false;

                    if (_intProductId > 0)
                    {
                        btnEdit.Enabled = true;
                        btnDelete.Enabled = true;
                        btnCopy.Enabled = true;
                        btnBOM.Enabled = chkMakeItem.Checked;
                        btnRouting.Enabled = chkMakeItem.Checked;
                        btnCost.Enabled = true;
                    }
                    else
                    {
                        btnEdit.Enabled = false;
                        btnDelete.Enabled = false;
                        btnCopy.Enabled = false;
                        btnBOM.Enabled = false;
                        btnRouting.Enabled = false;
                        btnCost.Enabled = false;
                    }
                    break;
            }
        }

        private void LockForm(bool blnLock)
        {
            //general information
            txtRevision.Enabled = !blnLock;
            cboCCN.Enabled = !blnLock;

            //Tab Standard Information
            chkMakeItem.Enabled = !blnLock;

            cboStockUMID.Enabled = !blnLock;
            cboDesignUMID.Enabled = !blnLock;
            cboBuyingUMID.Enabled = !blnLock;
            cboSellingUMID.Enabled = !blnLock;
            cboCostMethod.Enabled = !blnLock;
            cboAccountCode.Enabled = !blnLock;
            chkQAStatus.Enabled = !blnLock;
            cboMasterLocationID.Enabled = !blnLock;

            if (cboMasterLocationID.SelectedIndex >= 0)
                cboLocationID.Enabled = !blnLock;
            else
                cboLocationID.Enabled = false;

            if (cboLocationID.SelectedIndex >= 0)
            {
                cboBinID.Enabled = !blnLock;
            }
            else
            {
                cboBinID.Enabled = false;
            }

            cboDeliveryPolicyID.Enabled = !blnLock;
            cboOrderPolicyID.Enabled = !blnLock;
            cboShipToleranceID.Enabled = !blnLock;
            cboCategoryID.Enabled = !blnLock;
            cboSourceID.Enabled = !blnLock;
            cboHazardID.Enabled = !blnLock;
            cboFreightClassID.Enabled = !blnLock;
            cboReasonID.Enabled = !blnLock;
            cboProductType.Enabled = !blnLock;
            cboFormatCodeID.Enabled = !blnLock;

            chkStock.Enabled = !blnLock;
            chkLotControl.Enabled = !blnLock;
            txtShelfLife.Enabled = !blnLock;
            txtVAT.Enabled = !blnLock;
            txtImportTax.Enabled = !blnLock;
            txtExportTax.Enabled = !blnLock;
            txtSpecialTax.Enabled = !blnLock;
            txtLotSize.Enabled = !blnLock;
            txtSetUpPair.Enabled = !blnLock;
            txtOtherInfo1.Enabled = !blnLock;
            txtOtherInfo2.Enabled = !blnLock;
            dtmSetupDate.Enabled = !blnLock;
            btnChangePicture.Enabled = !blnLock;
            btnClearPicture.Enabled = !blnLock;

            if (cboFormatCodeID.SelectedIndex >= 0)
            {
                txtPartNumber.Enabled = blnLock;
            }
            else
            {
                txtPartNumber.Enabled = !blnLock;
            }

            //Weight and Size
            grpWeightAndSize.Enabled = !blnLock;

            //Tab Planning data
            // grpPlantype.Enabled = !blnLock;
            grpReplenishment.Enabled = !blnLock;
            grpLeadTime.Enabled = !blnLock;

            chkAutoConversion.Enabled = !blnLock;

            //Begin_ Added by Tuan TQ-- 2005-09-22
            txtTaxCode.Enabled = !blnLock;
            txtGroup1.Enabled = !blnLock;
            txtGroup2.Enabled = !blnLock;
            txtGroupQuantity.Enabled = !blnLock;
            txtInventor.Enabled = !blnLock;
            btnInventor.Enabled = !blnLock;
            numLicenseFee.Enabled = !blnLock;
            cboProductType.Enabled = !blnLock;
            txtPartNameVN.Enabled = !blnLock;
            txtCurrency.Enabled = !blnLock;
            numPurchasingPrice.Enabled = !blnLock;
            numQuantitySet.Enabled = !blnLock;
            txtACAdjustment.Enabled = !blnLock;
            btnACAdjustment.Enabled = !blnLock;
            txtRegisteredCode.Enabled = !blnLock;
            chkAVEG.Enabled = !blnLock;
            chkMassOrder.Enabled = !blnLock;
            txtWorkcenter.Enabled = !blnLock;
            btnworkcenter.Enabled = !blnLock;
        }

        private void ClearForm()
        {
            //general information
            txtCode.Text = string.Empty;
            txtDescription.Text = string.Empty;
            txtRevision.Text = string.Empty;
            txtStockTakingCode.Text = string.Empty;
            cboCCN.Text = string.Empty;
            txtSetUpPair.Text = string.Empty;
            cboCCN.SelectedIndex = -1;
            //Tab Standard Information
            //Change to this Tab 
            //tabProductInfo.SelectedIndex = 0;

            txtRegisteredCode.Text = string.Empty;

            chkMakeItem.Checked = false;
            cboStockUMID.Text = string.Empty;
            cboStockUMID.SelectedIndex = -1;

            cboDesignUMID.Text = string.Empty;
            cboDesignUMID.SelectedIndex = -1;

            cboSellingUMID.Text = string.Empty;
            cboSellingUMID.SelectedIndex = -1;

            cboBuyingUMID.Text = string.Empty;
            cboBuyingUMID.SelectedIndex = -1;

            cboCostMethod.SelectedIndex = -1;
            cboCostMethod.Text = string.Empty;

            cboAccountCode.SelectedText = string.Empty;
            cboAccountCode.SelectedIndex = -1;

            chkQAStatus.Checked = false;

            cboMasterLocationID.Text = string.Empty;
            cboMasterLocationID.SelectedIndex = -1;
            cboLocationID.Text = string.Empty;
            cboLocationID.SelectedIndex = -1;
            cboBinID.Text = string.Empty;
            cboBinID.SelectedIndex = -1;
            txtShelfLife.Value = DBNull.Value;

            cboCategoryID.Text = string.Empty;
            cboCategoryID.SelectedIndex = -1;
            cboSourceID.Text = string.Empty;
            cboSourceID.SelectedIndex = -1;

            txtVAT.Value = DBNull.Value;
            txtImportTax.Value = DBNull.Value;
            txtExportTax.Value = DBNull.Value;
            txtSpecialTax.Value = DBNull.Value;

            cboHazardID.Text = string.Empty;
            cboHazardID.SelectedIndex = -1;
            cboFreightClassID.Text = string.Empty;
            cboFreightClassID.SelectedIndex = -1;
            cboReasonID.Text = string.Empty;
            cboReasonID.SelectedIndex = -1;

            chkStock.Checked = false;

            chkLotControl.Checked = false;
            txtLotSize.Value = DBNull.Value;
            cboFormatCodeID.Text = string.Empty;
            cboFormatCodeID.SelectedIndex = -1;
            txtOtherInfo1.Text = string.Empty;
            txtOtherInfo2.Text = string.Empty;

            //Weight and Size
            txtLength.Value = DBNull.Value;
            cboLengthUMID.Text = string.Empty;
            cboLengthUMID.SelectedIndex = -1;
            txtWidth.Value = DBNull.Value;
            cboWidthUMID.Text = string.Empty;
            cboWidthUMID.SelectedIndex = -1;
            txtHeight.Value = DBNull.Value;
            cboHeightUMID.Text = string.Empty;
            cboHeightUMID.SelectedIndex = -1;
            txtWeight.Value = DBNull.Value;
            cboWeightUMID.Text = string.Empty;
            cboWeightUMID.SelectedIndex = -1;
            txtRate.Value = DBNull.Value;

            dtmSetupDate.Value = DBNull.Value;

            cboDeliveryPolicyID.Text = string.Empty;
            cboDeliveryPolicyID.SelectedIndex = -1;

            cboOrderPolicyID.Text = string.Empty;
            cboOrderPolicyID.SelectedIndex = -1;

            cboShipToleranceID.Text = string.Empty;
            cboShipToleranceID.SelectedIndex = -1;
            chkAutoConversion.Checked = false;

            //Replenishment
            cboBuyerID.Text = string.Empty;
            cboBuyerID.SelectedIndex = -1;
            txtPrimaryVendorID.Text = string.Empty;
            txtPrimaryVendor.Text = string.Empty;
            cboVendorLocationID.Text = string.Empty;
            cboVendorLocationID.SelectedIndex = -1;
            cboOrderRuleID.Text = string.Empty;
            cboOrderRuleID.SelectedIndex = -1;

            txtOrderQuantity.Value = DBNull.Value;
            txtOrderQuantityMultiple.Value = DBNull.Value;
            txtOrderPoint.Value = DBNull.Value;

            txtSafetyStock.Value = DBNull.Value;
            txtScrapPercent.Value = DBNull.Value;
            txtMaximumStock.Value = DBNull.Value;
            txtMinimumStock.Value = DBNull.Value;
            txtIssueSize.Value = DBNull.Value;
            txtVoucherTolerance.Value = DBNull.Value;
            txtConversionTolerance.Value = DBNull.Value;
            chkRequisition.Checked = false;

            //Lead Time
            txtLTFixedTime.Value = DBNull.Value;
            txtLTSafetyStock.Value = DBNull.Value;
            txtLTVariableTime.Value = DBNull.Value;
            txtLTOrderPrepare.Value = DBNull.Value;
            txtLTDockToStock.Value = DBNull.Value;
            txtLTSalesATP.Value = DBNull.Value;
            txtLTShippingPrepare.Value = DBNull.Value;
            txtLTRequisition.Value = DBNull.Value;

            //Begin_ Added by Tuan TQ -2005-09-22
            ChangePrimaryVendorID();

            txtTaxCode.Text = string.Empty;
            txtPartNameVN.Text = string.Empty;
            txtInventor.Text = string.Empty;
            txtInventor.Tag = null;
            numLicenseFee.Value = DBNull.Value;
            cboProductType.Text = string.Empty;
            cboProductType.SelectedIndex = -1;
            txtCurrency.Text = string.Empty;
            txtCurrency.Tag = null;
            numPurchasingPrice.Value = DBNull.Value;
            numQuantitySet.Value = DBNull.Value;
            picCategory.Image = null;

            numMinProduce.Value = DBNull.Value;
            numMaxProduce.Value = DBNull.Value;

            numRoundUpToMin.Value = DBNull.Value;
            numRoundUpToMultiple.Value = DBNull.Value;
            txtACAdjustment.Tag = null;
            txtACAdjustment.Text = string.Empty;

            //End_ Added by Tuan TQ -2005-09-22
            txtVendorName.Text = string.Empty;

            txtWorkcenter.Text = string.Empty;
            lblWorkCenter.Tag = 0;

            if (SystemProperty.CCNID > 0)
            {
                cboCCN.SelectedValue = SystemProperty.CCNID;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveToDatabase())
            {
                //Turn to Add action
                _enumAction = EnumAction.Default;

                //lock form
                LockForm(true);

                //Enable Button
                EnableDisableButtons();

                PCSMessageBox.Show(ErrorCode.MESSAGE_AFTER_SAVE_DATA, MessageBoxIcon.Information);

                txtCode.Modified = false;
                txtDescription.Modified = false;
                btnAdd.Enabled = btnEdit.Enabled = btnDelete.Enabled = true;
                txtCode.Focus();
            }
        }

        private ITM_Product AssignValueToVOClass()
        {
            //Init the VO class
            var objITM_ProductVO = new ITM_Product();

            //Product ID            
            objITM_ProductVO.CreateDateTime = DateTime.Parse(dtmSetupDate.Value.ToString());
            objITM_ProductVO.ProductID = _intProductId;
            //Code
            objITM_ProductVO.Code = txtCode.Text.Trim();
            objITM_ProductVO.Description = txtDescription.Text.Trim();
            objITM_ProductVO.Revision = txtRevision.Text.Trim();
            objITM_ProductVO.StockTakingCode = txtStockTakingCode.Text.Trim();
            objITM_ProductVO.SetUpPair = txtSetUpPair.Text;
            objITM_ProductVO.RegisteredCode = txtRegisteredCode.Text.Trim();
            if (!string.IsNullOrEmpty(txtWorkcenter.Text.Trim()))
            {
                if (txtWorkcenter.Tag != null)
                {
                    objITM_ProductVO.WorkCenterID = int.Parse(txtWorkcenter.Tag.ToString());
                }
            }
            else
                objITM_ProductVO.WorkCenterID = null;

            //CCN
            if (cboCCN.SelectedIndex >= 0)
                objITM_ProductVO.CCNID = int.Parse(cboCCN.SelectedValue.ToString());
            //Make Item
            objITM_ProductVO.MakeItem = chkMakeItem.Checked;
            //Category 
            if (cboCategoryID.SelectedIndex >= 0 && cboCategoryID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.CategoryID = int.Parse(cboCategoryID.SelectedValue.ToString());
            else objITM_ProductVO.CategoryID = null;
            //Lot Control
            objITM_ProductVO.LotControl = chkLotControl.Checked;
            //Stock Unit Of measure
            if (cboStockUMID.SelectedIndex >= 0 && cboStockUMID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.StockUMID = int.Parse(cboStockUMID.SelectedValue.ToString());
            //Stock Unit Of measure
            if (cboDesignUMID.SelectedIndex >= 0 && cboDesignUMID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.DesignUMID = int.Parse(cboDesignUMID.SelectedValue.ToString());
            else objITM_ProductVO.DesignUMID = null;
            //Buying Unit of measure
            if (cboBuyingUMID.SelectedIndex >= 0 && cboBuyingUMID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.BuyingUMID = int.Parse(cboBuyingUMID.SelectedValue.ToString());
            //Selling Unit of measure
            if (cboSellingUMID.SelectedIndex >= 0 && cboSellingUMID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.SellingUMID = int.Parse(cboSellingUMID.SelectedValue.ToString());
            //Cost method
            if (cboCostMethod.SelectedIndex >= 0 && cboCostMethod.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.CostMethod = byte.Parse(cboCostMethod.SelectedValue.ToString());
            else
                objITM_ProductVO.CostMethod = null;
            //AGC
            if (cboAccountCode.SelectedIndex >= 0 && cboAccountCode.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.AGCID = int.Parse(cboAccountCode.SelectedValue.ToString().Trim());
            else
                objITM_ProductVO.AGCID = null;
            //QA status
            if (chkQAStatus.Checked)
                objITM_ProductVO.QAStatus = (int) EnumQA.REQUIRE_INSPECTION;
            else
                objITM_ProductVO.QAStatus = (int) EnumQA.NOT_REQUIRE_INSPECTION;
            //Master Location
            if (cboMasterLocationID.SelectedIndex >= 0 &&
                cboMasterLocationID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.MasterLocationID = int.Parse(cboMasterLocationID.SelectedValue.ToString());
            else objITM_ProductVO.MasterLocationID = null;
            //Location
            if (cboLocationID.SelectedIndex >= 0 && cboLocationID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.LocationID = int.Parse(cboLocationID.SelectedValue.ToString());
            //Bin
            if (cboBinID.SelectedIndex >= 0 && cboBinID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.BinID = int.Parse(cboBinID.SelectedValue.ToString());
            //Shelf Life
            if (txtShelfLife.Text.Trim() != string.Empty && txtShelfLife.Value != DBNull.Value)
                objITM_ProductVO.ShelfLife = Decimal.Parse(txtShelfLife.Text);
            else
                objITM_ProductVO.ShelfLife = 0;
            //Source 
            if (cboSourceID.SelectedIndex >= 0 && cboSourceID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.SourceID = int.Parse(cboSourceID.SelectedValue.ToString());
            else
                objITM_ProductVO.SourceID = null;
            //VAT
            if (txtVAT.Text.Trim() != string.Empty && txtVAT.Value != DBNull.Value)
                objITM_ProductVO.VAT = double.Parse(txtVAT.Text);
            else
                objITM_ProductVO.VAT = null;
            //Import Tax
            if (txtImportTax.Text.Trim() != string.Empty && txtImportTax.Value != DBNull.Value)
                objITM_ProductVO.ImportTax = double.Parse(txtImportTax.Text);
            else
                objITM_ProductVO.ImportTax = null;
            //Export Tax
            if (txtExportTax.Text.Trim() != string.Empty && txtExportTax.Value != DBNull.Value)
                objITM_ProductVO.ExportTax = float.Parse(txtExportTax.Text);
            else
                objITM_ProductVO.ExportTax = null;
            //Special Tax
            if (txtSpecialTax.Text.Trim() != string.Empty && txtSpecialTax.Value != DBNull.Value)
                objITM_ProductVO.SpecialTax = double.Parse(txtSpecialTax.Text);
            else
                objITM_ProductVO.SpecialTax = null;
            //Hazard 
            if (cboHazardID.SelectedIndex >= 0 && cboHazardID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.HazardID = int.Parse(cboHazardID.SelectedValue.ToString());
            else objITM_ProductVO.HazardID = null;
            //Freight Class 
            if (cboFreightClassID.SelectedIndex >= 0 &&
                cboFreightClassID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.FreightClassID = int.Parse(cboFreightClassID.SelectedValue.ToString());
            else objITM_ProductVO.FreightClassID = null;
            //Delete Reason 
            if (cboReasonID.SelectedIndex >= 0 && cboReasonID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.DeleteReasonID = int.Parse(cboReasonID.SelectedValue.ToString());
            else objITM_ProductVO.DeleteReasonID = null;
            //Stock
            objITM_ProductVO.Stock = chkStock.Checked;
            //Lot Size
            if (txtLotSize.Text.Trim() != string.Empty && txtLotSize.Value != DBNull.Value)
                objITM_ProductVO.LotSize = int.Parse(txtLotSize.Value.ToString());
            else
                objITM_ProductVO.LotSize = null;
            //Format code
            if (cboFormatCodeID.SelectedIndex >= 0 && cboFormatCodeID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.FormatCodeID = int.Parse(cboFormatCodeID.SelectedValue.ToString());
            //Part number
            objITM_ProductVO.PartNumber = txtPartNumber.Text.Trim();
            //Other infor 1
            objITM_ProductVO.OtherInfo1 = txtOtherInfo1.Text.Trim();
            //Other infor 2
            objITM_ProductVO.OtherInfo2 = txtOtherInfo2.Text.Trim();
            //Length
            if (txtLength.Text.Trim() != string.Empty && txtLength.Value != DBNull.Value)
                objITM_ProductVO.Length = Decimal.Parse(txtLength.Text);
            else
                objITM_ProductVO.Length = 0;
            if (cboLengthUMID.SelectedIndex >= 0 && cboLengthUMID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.LengthUMID = int.Parse(cboLengthUMID.SelectedValue.ToString());
            //Width 
            if (txtWidth.Text.Trim() != string.Empty && txtWidth.Value != DBNull.Value)
                objITM_ProductVO.Width = Decimal.Parse(txtWidth.Text);
            else
                objITM_ProductVO.Width = 0;
            if (cboWidthUMID.SelectedIndex >= 0 && cboWidthUMID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.WidthUMID = int.Parse(cboWidthUMID.SelectedValue.ToString());
            //Height 
            if (txtHeight.Text.Trim() != string.Empty && txtHeight.Value != DBNull.Value)
                objITM_ProductVO.Height = Decimal.Parse(txtHeight.Text);
            else
                objITM_ProductVO.Height = 0;
            if (cboHeightUMID.SelectedIndex >= 0 && cboHeightUMID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.HeightUMID = int.Parse(cboHeightUMID.SelectedValue.ToString());
            //Weight
            if (txtWeight.Text.Trim() != string.Empty && txtWeight.Value != DBNull.Value)
                objITM_ProductVO.Weight = Decimal.Parse(txtWeight.Text);
            else
                objITM_ProductVO.Weight = 0;

            //Rate
            try
            {
                var rate = Convert.ToDecimal(txtRate.Value);
                objITM_ProductVO.Rate = rate > 0 ? (decimal?) rate : null;
            }
            catch
            {
                objITM_ProductVO.Rate = null;
            }

            if (cboWeightUMID.SelectedIndex >= 0 && cboWeightUMID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.WeightUMID = int.Parse(cboWeightUMID.SelectedValue.ToString());
            //Setup Date
            if (dtmSetupDate.Value != DBNull.Value && dtmSetupDate.Text.Trim() != string.Empty)
                objITM_ProductVO.SetupDate = (DateTime) dtmSetupDate.Value;
            else
                objITM_ProductVO.SetupDate = DateTime.MinValue;
            //Plan Type
            if (radPlanTypeMPS.Checked)
                objITM_ProductVO.PlanType = (int) PlanTypeEnum.MPS;
            if (radPlanTypeMRP.Checked)
                objITM_ProductVO.PlanType = (int) PlanTypeEnum.MRP;
            //Delivery policy
            if (cboDeliveryPolicyID.SelectedIndex >= 0 &&
                cboDeliveryPolicyID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.DeliveryPolicyID = int.Parse(cboDeliveryPolicyID.SelectedValue.ToString());
            //Order Policy
            if (cboOrderPolicyID.SelectedIndex >= 0 && cboOrderPolicyID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.OrderPolicyID = int.Parse(cboOrderPolicyID.SelectedValue.ToString());
            //Ship Tolerence 
            if (cboShipToleranceID.SelectedIndex >= 0 &&
                cboShipToleranceID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.ShipToleranceID = int.Parse(cboShipToleranceID.SelectedValue.ToString());
            //Auto Conversion 
            objITM_ProductVO.AutoConversion = chkAutoConversion.Checked;
            //Buyer
            if (cboBuyerID.SelectedIndex >= 0 && cboBuyerID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.BuyerID = int.Parse(cboBuyerID.SelectedValue.ToString());
            //Primary Vendor
            if (txtPrimaryVendorID.Text.Trim() != string.Empty)
                objITM_ProductVO.PrimaryVendorID = int.Parse(txtPrimaryVendorID.Text.Trim());
            else objITM_ProductVO.PrimaryVendorID = null;
            //Vendor Location
            if (cboVendorLocationID.SelectedIndex >= 0 &&
                cboVendorLocationID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.VendorLocationID = int.Parse(cboVendorLocationID.SelectedValue.ToString());
            else objITM_ProductVO.VendorLocationID = null;
            //Order Rule
            if (cboOrderRuleID.SelectedIndex >= 0 && cboOrderRuleID.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.OrderRuleID = int.Parse(cboOrderRuleID.SelectedValue.ToString());
            //Min Oder Quantity
            objITM_ProductVO.OrderQuantity = txtOrderQuantity.Text.Trim() != string.Empty
                                                 ? Decimal.Parse(txtOrderQuantity.Text)
                                                 : 0;
            //Order Multiple Quantity
            if (txtOrderQuantityMultiple.Text.Trim() != string.Empty && txtOrderQuantityMultiple.Value != DBNull.Value)
                objITM_ProductVO.OrderQuantityMultiple = Decimal.Parse(txtOrderQuantityMultiple.Text);
            else
                objITM_ProductVO.OrderQuantityMultiple = 0;
            //Order point
            if (txtOrderPoint.Text.Trim() != string.Empty && txtOrderPoint.Value != DBNull.Value)
                objITM_ProductVO.OrderPoint = Decimal.Parse(txtOrderPoint.Text);
            else
                objITM_ProductVO.OrderPoint = 0;
            //Safety stock
            if (txtSafetyStock.Text.Trim() != string.Empty && txtSafetyStock.Value != DBNull.Value)
                objITM_ProductVO.SafetyStock = Decimal.Parse(txtSafetyStock.Text);
            else
                objITM_ProductVO.SafetyStock = 0;
            //Scrapt Percent
            if (txtScrapPercent.Text.Trim() != string.Empty && txtScrapPercent.Value != DBNull.Value)
                objITM_ProductVO.ScrapPercent = double.Parse(txtScrapPercent.Text);
            else
                objITM_ProductVO.ScrapPercent = 0;
            //Maximum Stock
            if (txtMaximumStock.Text.Trim() != string.Empty && txtMaximumStock.Value != DBNull.Value)
                objITM_ProductVO.MaximumStock = Decimal.Parse(txtMaximumStock.Text);
            else
                objITM_ProductVO.MaximumStock = 0;
            //Minimum stock
            if (txtMinimumStock.Text.Trim() != string.Empty && txtMinimumStock.Value != DBNull.Value)
                objITM_ProductVO.MinimumStock = Decimal.Parse(txtMinimumStock.Text);
            else
                objITM_ProductVO.MinimumStock = 0;
            //Issue Size
            if (txtIssueSize.Text.Trim() != string.Empty && txtIssueSize.Value != DBNull.Value)
                objITM_ProductVO.IssueSize = Decimal.Parse(txtIssueSize.Text);
            else
                objITM_ProductVO.IssueSize = 0;
            //Voucher tolerence
            if (txtVoucherTolerance.Text.Trim() != string.Empty && txtVoucherTolerance.Value != DBNull.Value)
                objITM_ProductVO.VoucherTolerance = Decimal.Parse(txtVoucherTolerance.Text);
            else
                objITM_ProductVO.VoucherTolerance = 0;
            //Conversion Tolerence
            if (txtConversionTolerance.Text.Trim() != string.Empty && txtConversionTolerance.Value != DBNull.Value)
                objITM_ProductVO.ConversionTolerance = Decimal.Parse(txtConversionTolerance.Text);
            else
                objITM_ProductVO.ConversionTolerance = 0;
            //Lead Time Fixed Time
            if (txtLTFixedTime.Text.Trim() != string.Empty && txtLTFixedTime.Value != DBNull.Value)
                objITM_ProductVO.LTFixedTime = Decimal.Parse(txtLTFixedTime.Text);
            else
                objITM_ProductVO.LTFixedTime = 0;
            //LeadTime Safety Stock
            if (txtLTSafetyStock.Text.Trim() != string.Empty && txtLTSafetyStock.Value != DBNull.Value)
                objITM_ProductVO.LTSafetyStock = Decimal.Parse(txtLTSafetyStock.Text);
            else
                objITM_ProductVO.LTSafetyStock = 0;
            //LTVariable
            if (txtLTVariableTime.Text.Trim() != string.Empty && txtLTVariableTime.Value != DBNull.Value)
                objITM_ProductVO.LTVariableTime = Decimal.Parse(txtLTVariableTime.Text);
            else
                objITM_ProductVO.LTVariableTime = 0;
            //LT Order Prepare
            if (txtLTOrderPrepare.Text.Trim() != string.Empty && txtLTOrderPrepare.Value != DBNull.Value)
                objITM_ProductVO.LTOrderPrepare = Decimal.Parse(txtLTOrderPrepare.Text);
            else
                objITM_ProductVO.LTOrderPrepare = 0;
            //LTDocToStock
            if (txtLTDockToStock.Text.Trim() != string.Empty && txtLTDockToStock.Value != DBNull.Value)
                objITM_ProductVO.LTDockToStock = Decimal.Parse(txtLTDockToStock.Text);
            else
                objITM_ProductVO.LTDockToStock = 0;
            //LTSales ATP
            if (txtLTSalesATP.Text.Trim() != string.Empty && txtLTSalesATP.Value != DBNull.Value)
                objITM_ProductVO.LTSalesATP = Decimal.Parse(txtLTSalesATP.Text);
            else
                objITM_ProductVO.LTSalesATP = 0;
            //LTShip Prepare
            if (txtLTShippingPrepare.Text.Trim() != string.Empty && txtLTShippingPrepare.Value != DBNull.Value)
                objITM_ProductVO.LTShippingPrepare = Decimal.Parse(txtLTShippingPrepare.Text);
            else
                objITM_ProductVO.LTShippingPrepare = 0;
            //LT Requisition
            if (txtLTRequisition.Text.Trim() != string.Empty && txtLTRequisition.Value != DBNull.Value)
                objITM_ProductVO.LTRequisition = Decimal.Parse(txtLTRequisition.Text);
            else
                objITM_ProductVO.LTRequisition = 0;
            //Begin_ Added by Tuan TQ
            //Part name VN
            objITM_ProductVO.PartNameVN = txtPartNameVN.Text.Trim();
            //Tax code
            objITM_ProductVO.TaxCode = txtTaxCode.Text.Trim();
            // License Fee
            if (numLicenseFee.Text.Trim() != string.Empty && numLicenseFee.Value != DBNull.Value)
            {
                objITM_ProductVO.LicenseFee = FormControlComponents.IsPositiveNumeric(numLicenseFee.Value.ToString())
                                                  ? decimal.Parse(numLicenseFee.Value.ToString())
                                                  : 0;
            }
            else
                objITM_ProductVO.LicenseFee = 0;

            // List price
            if (numPurchasingPrice.Text.Trim() != string.Empty && numPurchasingPrice.Value != DBNull.Value)
                objITM_ProductVO.ListPrice = decimal.Parse(numPurchasingPrice.Value.ToString());
            else
                objITM_ProductVO.ListPrice = 0;
            // Min produce
            if (numMinProduce.Text.Trim() != string.Empty && numMinProduce.Value != DBNull.Value)
                objITM_ProductVO.MinProduce = decimal.Parse(numMinProduce.Value.ToString());
            else
                objITM_ProductVO.MinProduce = 0;
            // max produce
            if (numMaxProduce.Text.Trim() != string.Empty && numMaxProduce.Value != DBNull.Value)
                objITM_ProductVO.MaxProduce = decimal.Parse(numMaxProduce.Value.ToString());
            else
                objITM_ProductVO.MaxProduce = 0;
            // Quantity Set
            if (numQuantitySet.Text.Trim() != string.Empty && numQuantitySet.Value != DBNull.Value)
                objITM_ProductVO.QuantitySet = decimal.Parse(numQuantitySet.Value.ToString());
            else
                objITM_ProductVO.QuantitySet = 0;

            // Max Round Up To Min
            if (numRoundUpToMin.Text.Trim() != string.Empty && numRoundUpToMin.Value != DBNull.Value)
                objITM_ProductVO.MaxRoundUpToMin = decimal.Parse(numRoundUpToMin.Value.ToString());
            else
                objITM_ProductVO.MaxRoundUpToMin = 0;

            // Max Round Up To Multiple
            if (numRoundUpToMultiple.Text.Trim() != string.Empty && numRoundUpToMultiple.Value != DBNull.Value)
                objITM_ProductVO.MaxRoundUpToMultiple = decimal.Parse(numRoundUpToMultiple.Value.ToString());
            else
                objITM_ProductVO.MaxRoundUpToMultiple = 0;

            //CurrencyID
            if (txtCurrency.Text.Trim() != string.Empty)
                objITM_ProductVO.VendorCurrencyID = int.Parse(txtCurrency.Tag.ToString());
            else
                objITM_ProductVO.VendorCurrencyID = null;

            //AC Adjustment ID
            if (txtACAdjustment.Text.Trim() != string.Empty)
                objITM_ProductVO.ACAdjustmentMasterID = int.Parse(txtACAdjustment.Tag.ToString());
            else
                objITM_ProductVO.ACAdjustmentMasterID = null;

            //Inventor
            if (txtInventor.Text.Trim() != string.Empty)
                objITM_ProductVO.InventorID = int.Parse(txtInventor.Tag.ToString());
            else
                objITM_ProductVO.InventorID = null;
            //Product Type
            if (cboProductType.SelectedIndex >= 0 && cboProductType.SelectedValue.ToString().Trim() != string.Empty)
                objITM_ProductVO.ProductTypeID = int.Parse(cboProductType.SelectedValue.ToString());
            else
                objITM_ProductVO.ProductTypeID = null;
            //End_ Added by Tuan TQ
            objITM_ProductVO.AVEG = chkAVEG.Checked;
            objITM_ProductVO.MassOrder = chkMassOrder.Checked;
            objITM_ProductVO.Group1 = txtGroup1.Text;
            objITM_ProductVO.Group2 = txtGroup2.Text;
            try
            {
                objITM_ProductVO.GroupQuantity = Convert.ToDecimal(txtGroupQuantity.Value);
            }
            catch
            {
                objITM_ProductVO.GroupQuantity = 0;
            }
            objITM_ProductVO.ProductionLineID = _producttionLineId;

            return objITM_ProductVO;
        }

        private bool SaveToDatabase()
        {
            _dataIsValid = ValidateMandatoryControl();
            if (!_dataIsValid)
            {
                return false;
            }

            //Init the BO class
            var objProductItemInfoBO = new ProductItemInfoBO();

            //HACK: added by Tuan TQ: 31 Mar, 2006
            _voProduct = AssignValueToVOClass();
            //End hack

            switch (_enumAction)
            {
                case EnumAction.Add:
                    _voProduct.ProductID = 0;
                    _intProductId = objProductItemInfoBO.InsertProduct(_voProduct);
                    if (_copiedProductId > 0)
                    {
                        DataSet dsetBOM = objProductItemInfoBO.GetListBomByproductId(_copiedProductId);
                        DataSet dsetRounting = objProductItemInfoBO.GetListRountingByproductId(_copiedProductId);
                        if (dsetBOM != null)
                        {
                            //Copy Bom
                            if (dsetBOM.Tables.Count > 0)
                            {
                                foreach (DataRow dr in dsetBOM.Tables[0].Rows)
                                {
                                    var objBomVO = new ITM_BOM();
                                    objBomVO.Alternative = null;
                                    objBomVO.Ancestor = "";
                                    objBomVO.BomID = 0;
                                    if (dr["BomUMID"] != DBNull.Value)
                                    {
                                        objBomVO.BomUMID = Convert.ToInt32(dr["BomUMID"].ToString());
                                    }
                                    if (dr["ComponentID"] != DBNull.Value)
                                    {
                                        objBomVO.ComponentID = Convert.ToInt32(dr["ComponentID"].ToString());
                                    }
                                    if (dr["Line"] != DBNull.Value)
                                    {
                                        objBomVO.Line = Convert.ToInt32(dr["Line"].ToString());
                                    }
                                    if (_copiedProductId > 0)
                                    {
                                        objBomVO.ProductID = _intProductId;
                                    }
                                    if (dr["Quantity"] != DBNull.Value)
                                    {
                                        objBomVO.Quantity = Convert.ToDecimal(dr["Quantity"].ToString());
                                    }
                                    objProductItemInfoBO.CopyBom(objBomVO);
                                }
                            }
                        }

                        //Copy Rounting
                        if (dsetRounting != null && dsetRounting.Tables.Count > 0)
                        {
                            foreach (DataRow dr in dsetRounting.Tables[0].Rows)
                            {
                                var objRountingVO = new ITM_Routing {ProductID = _intProductId};
                                //CalculateTime
                                if (dr["LaborRunTime"] != DBNull.Value)
                                {
                                    objRountingVO.LaborRunTime = Convert.ToDecimal(dr["LaborRunTime"]);
                                }
                                if (dr["WorkCenterID"] != DBNull.Value)
                                {
                                    objRountingVO.WorkCenterID = Convert.ToInt32(dr["WorkCenterID"]);
                                }
                                if (dr["CalculateTime"] != DBNull.Value)
                                {
                                    objRountingVO.CalculateTime = Convert.ToBoolean(dr["CalculateTime"]);
                                }
                                objProductItemInfoBO.CopyRounting(objRountingVO);
                            }
                        }
                    }
                    break;

                case EnumAction.Edit:
                    objProductItemInfoBO.UpdateProduct(_voProduct);
                    break;
            }

            _copiedProductId = 0;
            return true;
        }

        private bool ValidateMandatoryControl()
        {
            var objProductItemInfoBO = new ProductItemInfoBO();

            if (txtCode.Text.Trim() == string.Empty)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                txtCode.Focus();
                return false;
            }

            if (txtRevision.Text.Trim() == string.Empty)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                txtRevision.Focus();
                return false;
            }

            if (txtDescription.Text.Trim() == string.Empty)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                txtDescription.Focus();
                return false;
            }

            if (cboCCN.SelectedIndex < 0)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                cboCCN.Focus();
                return false;
            }

            // check for unique Stock Taking Code
            if (txtStockTakingCode.Text.Trim() != string.Empty)
            {
                int intProductID = 0;
                if (_enumAction == EnumAction.Edit)
                    intProductID = _voProduct.ProductID;
                if (!objProductItemInfoBO.CheckUniqueStockTakingCode(intProductID, txtStockTakingCode.Text.Trim()))
                {
                    PCSMessageBox.Show(ErrorCode.DUPLICATE_KEY, MessageBoxIcon.Warning);
                    txtStockTakingCode.Focus();
                    return false;
                }
            }

            if (cboStockUMID.SelectedIndex < 0 || cboStockUMID.SelectedValue.ToString().Trim() == string.Empty)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                cboStockUMID.Focus();
                return false;
            }

            if (cboDesignUMID.SelectedIndex < 0 || cboDesignUMID.SelectedValue.ToString().Trim() == string.Empty)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                cboDesignUMID.Focus();
                return false;
            }

            if (cboBuyingUMID.SelectedIndex < 0 || cboBuyingUMID.SelectedValue.ToString().Trim() == string.Empty)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                cboBuyingUMID.Focus();
                return false;
            }

            if (cboSellingUMID.SelectedIndex < 0 || cboSellingUMID.SelectedValue.ToString().Trim() == string.Empty)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                cboSellingUMID.Focus();
                return false;
            }

            // HACK: Trada 09-02-2006
            if (cboMasterLocationID.SelectedIndex > 0)
            {
                string[] arrMessage;
                if (cboLocationID.SelectedIndex < 0 || cboLocationID.SelectedValue.ToString().Trim() == string.Empty)
                {
                    arrMessage = new[] {lblLocationID.Text};
                    PCSMessageBox.Show(ErrorCode.MESSAGE_MANDATORY_FIELD_REQUIRED, MessageBoxIcon.Warning, arrMessage);

                    tabProductInfo.SelectedIndex = 0;
                    cboLocationID.Focus();
                    return false;
                }

                if (cboBinID.SelectedIndex < 0 || cboBinID.SelectedValue.ToString().Trim() == string.Empty)
                {
                    arrMessage = new[] {lblBinID.Text};

                    PCSMessageBox.Show(ErrorCode.MESSAGE_MANDATORY_FIELD_REQUIRED, MessageBoxIcon.Warning, arrMessage);
                    tabProductInfo.SelectedIndex = 0;
                    cboBinID.Focus();
                    return false;
                }
            }
            // END: Trada 09-02-2006

            //set up date
            if (dtmSetupDate.Value == DBNull.Value || dtmSetupDate.Text.Trim() == string.Empty)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                dtmSetupDate.Focus();
                return false;
            }
            var strParams = new string[2];

            //length
            if ((txtLength.Text.Trim() != string.Empty) && Decimal.Parse(txtLength.Text) > 0)
            {
                strParams[0] = lblLengthUMID.Text;
                strParams[1] = lblLength.Text;
                if (cboLengthUMID.SelectedIndex < 0 || cboLengthUMID.SelectedValue.ToString().Trim() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RELATION_REQUIRE, MessageBoxIcon.Error, strParams);
                    tabProductInfo.SelectedIndex = 0;
                    cboLengthUMID.Focus();
                    return false;
                }
            }

            // HACK : TuanDM 2005 - 13- 10
            if (cboLengthUMID.SelectedIndex > 0)
            {
                strParams[1] = lblLengthUMID.Text;
                strParams[0] = lblLength.Text;
                if (txtLength.Value.ToString() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RELATION_REQUIRE, MessageBoxIcon.Error, strParams);
                    tabProductInfo.SelectedIndex = 0;
                    txtLength.Focus();
                    return false;
                }
            }
            // End : TuanDM 2005 - 13- 10
            //height
            if ((txtHeight.Text.Trim() != string.Empty) && Decimal.Parse(txtHeight.Text) > 0)
            {
                strParams[0] = lblHeightUMID.Text;
                strParams[1] = lblHeight.Text;
                if (cboHeightUMID.SelectedIndex < 0 || cboHeightUMID.SelectedValue.ToString().Trim() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RELATION_REQUIRE, MessageBoxIcon.Error, strParams);
                    tabProductInfo.SelectedIndex = 0;
                    cboHeightUMID.Focus();
                    return false;
                }
            }
            // HACK : TuanDM 2005 - 13- 10
            if (cboHeightUMID.SelectedIndex > 0)
            {
                strParams[1] = lblHeightUMID.Text;
                strParams[0] = lblHeight.Text;
                if (txtHeight.Value.ToString() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RELATION_REQUIRE, MessageBoxIcon.Error, strParams);
                    tabProductInfo.SelectedIndex = 0;
                    txtHeight.Focus();
                    return false;
                }
            }
            // End : TuanDM 2005 - 13- 10
            //Width 
            if ((txtWidth.Text.Trim() != string.Empty) && Decimal.Parse(txtWidth.Text) > 0)
            {
                strParams[0] = lblWidthUMID.Text;
                strParams[1] = lblWidth.Text;
                if (cboWidthUMID.SelectedIndex < 0 || cboWidthUMID.SelectedValue.ToString().Trim() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RELATION_REQUIRE, MessageBoxIcon.Error, strParams);
                    tabProductInfo.SelectedIndex = 0;
                    cboWidthUMID.Focus();
                    return false;
                }
            }
            // HACK : TuanDM 2005 - 13- 10
            if (cboWidthUMID.SelectedIndex > 0)
            {
                strParams[1] = lblWidthUMID.Text;
                strParams[0] = lblWidth.Text;
                if (txtWidth.Value.ToString() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RELATION_REQUIRE, MessageBoxIcon.Error, strParams);
                    tabProductInfo.SelectedIndex = 0;
                    txtWidth.Focus();
                    return false;
                }
            }
            // End : TuanDM 2005 - 13- 10
            //Weight
            if ((txtWeight.Text.Trim() != string.Empty) && Decimal.Parse(txtWeight.Text) > 0)
            {
                strParams[0] = lblWeightUMID.Text;
                strParams[1] = lblWeight.Text;
                if (cboWeightUMID.SelectedIndex < 0 || cboWeightUMID.SelectedValue.ToString().Trim() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RELATION_REQUIRE, MessageBoxIcon.Error, strParams);
                    tabProductInfo.SelectedIndex = 0;
                    cboWeightUMID.Focus();
                    return false;
                }
            }
            // HACK : TuanDM 2005 - 13- 10
            if (cboWeightUMID.SelectedIndex > 0)
            {
                strParams[1] = lblWeightUMID.Text;
                strParams[0] = lblWeight.Text;
                if (txtWeight.Value.ToString() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RELATION_REQUIRE, MessageBoxIcon.Error, strParams);
                    tabProductInfo.SelectedIndex = 0;
                    txtWeight.Focus();
                    return false;
                }
            }
            // End : TuanDM 2005 - 13- 10
            //check MinStoc and MaxStock
            if (txtMinimumStock.Text.Trim() != string.Empty)
            {
                if (txtMaximumStock.Text.Trim() == string.Empty)
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                    tabProductInfo.SelectedIndex = 1;
                    txtMaximumStock.Focus();
                    return false;
                }
                if (decimal.Parse(txtMinimumStock.Text.Trim()) > decimal.Parse(txtMaximumStock.Text.Trim()))
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_INVALID_MINIMUMSTOCK, MessageBoxIcon.Warning);
                    tabProductInfo.SelectedIndex = 1;
                    txtMinimumStock.Focus();
                    return false;
                }
            }

            // HACK : TUANDM 2005 - 13 - 10 
            if (txtInventor.Text != string.Empty && txtInventor.Tag == null)
            {
                PCSMessageBox.Show(ErrorCode.MESSAGE_INVALID_INVENTOR, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                txtInventor.Focus();
                return false;
            }

            // END : TUANDM 2005 - 13 - 10

            //Check Unit Buying and Stock 
            if (cboStockUMID.SelectedValue.ToString() != cboBuyingUMID.SelectedValue.ToString())
            {
                if (
                    !objProductItemInfoBO.isTwoUnitOfMeasureScalled(int.Parse(cboBuyingUMID.SelectedValue.ToString()),
                                                                    int.Parse(cboStockUMID.SelectedValue.ToString())))
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_INVALID_STOCKUNIT_AND_BUYINGUNIT, MessageBoxIcon.Warning);
                    tabProductInfo.SelectedIndex = 0;
                    cboBuyingUMID.Focus();
                    return false;
                }
            }

            //Check Unit Selling and Stock 
            if (cboStockUMID.SelectedValue.ToString() != cboSellingUMID.SelectedValue.ToString())
            {
                if (
                    !objProductItemInfoBO.isTwoUnitOfMeasureScalled(int.Parse(cboSellingUMID.SelectedValue.ToString()),
                                                                    int.Parse(cboStockUMID.SelectedValue.ToString())))
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_INVALID_STOCKUNIT_AND_SELLINGUNIT, MessageBoxIcon.Warning);
                    tabProductInfo.SelectedIndex = 0;
                    cboSellingUMID.Focus();
                    return false;
                }
            }

            if (txtSetUpPair.Text.Trim() == 0.ToString())
            {
                PCSMessageBox.Show(ErrorCode.MESSAGE_DIFFERENT_ZERO, MessageBoxIcon.Warning);
                tabProductInfo.SelectedIndex = 0;
                txtSetUpPair.Focus();
                return false;
            }

            return true;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnEdit_Click()";
            if (_intProductId == -1)
            {
                PCSMessageBox.Show(ErrorCode.MESSAGE_PRODUCTINFO_SELECTPRODUCT, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                //Turn to Add action
                _enumAction = EnumAction.Edit;

                //Unlock form
                LockForm(false);

                //Enable Button
                EnableDisableButtons();

                //focus
                txtCode.Focus();
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnDelete_Click()";
            if (_intProductId == -1)
            {
                PCSMessageBox.Show(ErrorCode.MESSAGE_PRODUCTINFO_SELECTPRODUCT, MessageBoxIcon.Warning);
                return;
            }
            if (PCSMessageBox.Show(ErrorCode.MESSAGE_DELETE_RECORD, MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                DialogResult.Yes)
            {
                try
                {
                    //call the BO class to delete
                    var objProductItemInfoBO = new ProductItemInfoBO();
                    objProductItemInfoBO.Delete(_intProductId);
                    //After deleting, clean environment
                    _intProductId = -1; //No product after deleting
                    //Turn to default action
                    _enumAction = EnumAction.Default;
                    //unlock form
                    LockForm(false);
                    //clear controls
                    ClearForm();
                    //lock form
                    LockForm(true);
                    //Enable Button
                    EnableDisableButtons();
                    
                    //focus
                    txtCode.Focus();
                }
                catch (PCSException ex)
                {
                    // displays the error message.
                    if (ex.mCode == ErrorCode.CASCADE_DELETE_PREVENT)
                    {
                        MessageBox.Show("Bạn không thể xóa được vì sản phẩm đã được sử dụng!", "Question",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                    }
                    // log message.
                    try
                    {
                        Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                    }
                    catch
                    {
                        PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                    }
                }
                catch (Exception ex)
                {
                    // displays the error message.
                    PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                    // log message.
                    try
                    {
                        Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                    }
                    catch
                    {
                        PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void ProductItemInfo_Closing(object sender, CancelEventArgs e)
        {
            if (_enumAction == EnumAction.Add || _enumAction == EnumAction.Edit)
            {
                DialogResult dlgResult = PCSMessageBox.Show(ErrorCode.MESSAGE_QUESTION_STORE_INTO_DATABASE,
                                                            MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                switch (dlgResult)
                {
                    case DialogResult.Yes:
                        SaveToDatabase();
                        e.Cancel = !_dataIsValid;
                        break;

                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                }
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnCopy_Click()";
            if (_intProductId <= 0)
            {
                PCSMessageBox.Show(ErrorCode.MESSAGE_PRODUCTINFO_SELECTPRODUCT, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                //Unlock form
                LockForm(false);

                //Clear some properties
                txtCode.Text = string.Empty;
                txtDescription.Text = string.Empty;
                txtRevision.Text = string.Empty;
                txtStockTakingCode.Text = string.Empty;
                txtPartNameVN.Text = string.Empty;
                txtWorkcenter.Text = string.Empty;
                lblWorkCenter.Tag = 0;
                // reset created date
                dtmSetupDate.Value = new UtilsBO().GetDBDate();

                //Change the status of the form
                _enumAction = EnumAction.Add;

                //Enable Buttons
                EnableDisableButtons();

                //store the current product ID , for later to copy it from
                _copiedProductId = _intProductId;
                txtCode.Focus();
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void cboStockUMID_TextChanged(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".cboBuyingUMID_TextChanged()";
            try
            {
                lblStockUM1.Text = "(" + cboStockUMID.Text + ")";
                lblStockUM2.Text = lblStockUM1.Text;
                lblStockUM3.Text = lblStockUM1.Text;
                lblStockUM4.Text = lblStockUM1.Text;
            }
            catch (Exception ex)
            {
                // displays the error message.

                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void ChangeMasterLocation()
        {
            const string METHOD_NAME = This + ".ChangeMasterLocation()";
            try
            {
                if (cboMasterLocationID.SelectedIndex < 0 ||
                    cboMasterLocationID.SelectedValue.ToString().Trim() == string.Empty)
                {
                    cboLocationID.SelectedIndex = -1;
                    cboBinID.SelectedIndex = -1;
                    cboLocationID.Enabled = false;
                    cboBinID.Enabled = false;
                }
                else
                {
                    if (_enumAction != EnumAction.Default)
                    {
                        cboLocationID.Enabled = true;
                    }
                    else
                    {
                        cboBinID.Enabled = false;
                    }

                    //filter all rows from BIN
                    DataView dvLocation = ((DataTable) cboLocationID.DataSource).DefaultView;
                    dvLocation.RowFilter = MST_LocationTable.MASTERLOCATIONID_FLD + " = " +
                                           cboMasterLocationID.SelectedValue;
                    cboLocationID.SelectedIndex = -1;
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void cboMasterLocationID_SelectedValueChanged(object sender, EventArgs e)
        {
            ChangeMasterLocation();
        }

        private void ChangeLocation()
        {
            const string METHOD_NAME = This + ".ChangeLocation()";
            try
            {
                if (cboLocationID.SelectedIndex < 0 || cboLocationID.SelectedValue.ToString().Trim() == string.Empty)
                {
                    cboBinID.SelectedIndex = -1;
                    cboBinID.Enabled = false;
                }
                else
                {
                    cboBinID.Enabled = true;
                    object objBinValue =
                        ((DataTable) cboLocationID.DataSource).Rows[cboLocationID.SelectedIndex + 1][
                            MST_LocationTable.BIN_FLD];
                    if (!objBinValue.Equals(DBNull.Value))
                    {
                        if (!bool.Parse(objBinValue.ToString()))
                        {
                            cboBinID.SelectedIndex = -1;
                            cboBinID.Enabled = false;
                            return;
                        }
                    }

                    cboBinID.Enabled = _enumAction != EnumAction.Default;

                    //filter all rows from BIN
                    DataView dvBin = ((DataTable) cboBinID.DataSource).DefaultView;
                    dvBin.RowFilter = MST_BINTable.LOCATIONID_FLD + " = " + cboLocationID.SelectedValue;
                    if (dvBin.Count > 0)
                    {
                        cboBinID.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void cboLocationID_SelectedValueChanged(object sender, EventArgs e)
        {
            ChangeLocation();
        }

        private void btnBOM_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnBOM_Click()";
            try
            {
                if (_intProductId > 0)
                {
                    var objBomForm = new Bom();
                    objBomForm.voProduct.ProductID = _intProductId;
                    objBomForm.ShowDialog();
                    LoadProductInfor(_intProductId);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void cboMasterLocationID_ItemChanged(object sender, EventArgs e)
        {
            ChangeMasterLocation();
        }

        private void cboLocationID_ItemChanged(object sender, EventArgs e)
        {
            ChangeLocation();
        }

        private void btnSearchProductCode_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnSearchProductCode_Click()";
            try
            {
                DataRowView drwResult = null;
                var htbConditon = new Hashtable();
                if (cboCCN.SelectedValue != null)
                {
                    htbConditon.Add(ITM_ProductTable.CCNID_FLD, cboCCN.SelectedValue.ToString());
                }
                else
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_CCN, MessageBoxIcon.Warning);
                    cboCCN.Focus();
                    return;
                }
                drwResult = FormControlComponents.OpenSearchForm(ITM_ProductTable.TABLE_NAME, ITM_ProductTable.CODE_FLD,
                                                                 txtCode.Text.Trim(), htbConditon);
                if (drwResult != null)
                {
                    _intProductId = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    LoadProductInfor(_intProductId);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void ChangePrimaryVendorID()
        {
            const string METHOD_NAME = This + ".ChangePrimaryVendorID()";

            try
            {
                DataView drDataView = ((DataTable) cboVendorLocationID.DataSource).DefaultView;
                string strFilterString = MST_PartyLocationTable.PARTYID_FLD + "=" + txtPrimaryVendorID.Text;
                if (!drDataView.RowFilter.Equals(strFilterString))
                {
                    if (txtPrimaryVendorID.Text.Trim() == string.Empty || txtPrimaryVendorID.Text.Equals(ZeroString))
                    {
                        drDataView.RowFilter = MST_PartyLocationTable.PARTYID_FLD + "=0";
                        cboVendorLocationID.Enabled = false;
                    }
                    else
                    {
                        drDataView.RowFilter = MST_PartyLocationTable.PARTYID_FLD + "=" + txtPrimaryVendorID.Text;
                        cboVendorLocationID.Enabled = true;
                    }
                    cboVendorLocationID.SelectedIndex = -1;
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void btnSearchProductDescription_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnSearchProductDescription_Click()";
            try
            {
                DataRowView drwResult = null;
                var htbConditon = new Hashtable();
                if (cboCCN.SelectedValue != null)
                {
                    htbConditon.Add(ITM_ProductTable.CCNID_FLD, cboCCN.SelectedValue.ToString());
                }
                else
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_CCN, MessageBoxIcon.Warning);
                    cboCCN.Focus();
                    return;
                }
                drwResult = FormControlComponents.OpenSearchForm(ITM_ProductTable.TABLE_NAME,
                                                                 ITM_ProductTable.DESCRIPTION_FLD,
                                                                 txtDescription.Text.Trim(), htbConditon);
                if (drwResult != null)
                {
                    _intProductId = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    LoadProductInfor(_intProductId);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtPrimaryVendor_KeyDown(object sender, KeyEventArgs e)
        {
            const string METHOD_NAME = This + ".txtPrimaryVendor_KeyDown()";

            try
            {
                if ((e.KeyCode == Keys.F4) && (btnSearchVendor.Enabled))
                {
                    btnSearchVendor_Click(e, new EventArgs());
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnSearchVendor_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnSearchVendor_Click()";
            const string VENDOR = "Vendor";
            try
            {
                DataRowView drwResult = null;
                var htbCondition = new Hashtable();

                htbCondition.Add(VENDOR, 1);
                drwResult = FormControlComponents.OpenSearchForm(VCustomerVendor, MST_PartyTable.CODE_FLD,
                                                                 txtPrimaryVendor.Text, htbCondition, true);
                if (drwResult != null)
                {
                    txtPrimaryVendorID.Text = drwResult[MST_PartyTable.PARTYID_FLD].ToString();
                    txtPrimaryVendor.Text = drwResult[MST_PartyTable.CODE_FLD].ToString();
                    txtVendorName.Text = drwResult[MST_PartyTable.NAME_FLD].ToString();
                }
                ChangePrimaryVendorID();
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtCode_Leave(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".txtCode_Leave()";
            //Only use when users use this to search for existing product
            try
            {
                FormControlComponents.OnLeaveControl(sender, e);
                if (!btnSearchProductCode.Enabled || !txtCode.Modified)
                {
                    return;
                }
                if (txtCode.Text.Trim() == string.Empty)
                {
                    _intProductId = -1;
                    ClearForm();
                    EnableDisableButtons();
                    return;
                }
                var htbConditon = new Hashtable();
                if (cboCCN.SelectedValue != null)
                {
                    htbConditon.Add(ITM_ProductTable.CCNID_FLD, cboCCN.SelectedValue.ToString());
                }
                else
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_CCN, MessageBoxIcon.Warning);
                    cboCCN.Focus();
                    return;
                }
                DataRowView drwResult = FormControlComponents.OpenSearchForm(ITM_ProductTable.TABLE_NAME, ITM_ProductTable.CODE_FLD,
                                                                             txtCode.Text.Trim(), htbConditon, false);
                if (drwResult != null)
                {
                    _intProductId = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    LockForm(false);
                    ClearForm();
                    LockForm(true);
                    EnableDisableButtons();
                    LoadProductInfor(_intProductId);
                }
                else
                {
                    txtCode.Focus();
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtDescription_Leave(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".txtDescription_Leave()";
            //Only use when users use this to search for existing product
            try
            {
                FormControlComponents.OnLeaveControl(sender, e);
                if (!btnSearchProductDescription.Enabled || !txtDescription.Modified)
                {
                    return;
                }
                if (txtDescription.Text.Trim() == string.Empty)
                {
                    _intProductId = -1;
                    ClearForm();
                    EnableDisableButtons();
                    return;
                }
                var htbConditon = new Hashtable();
                if (cboCCN.SelectedValue != null)
                {
                    htbConditon.Add(ITM_ProductTable.CCNID_FLD, cboCCN.SelectedValue.ToString());
                }
                else
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_CCN, MessageBoxIcon.Warning);
                    cboCCN.Focus();
                    return;
                }
                DataRowView drwResult = FormControlComponents.OpenSearchForm(ITM_ProductTable.TABLE_NAME, ITM_ProductTable.DESCRIPTION_FLD, txtDescription.Text.Trim(), htbConditon, false);
                if (drwResult != null)
                {
                    _intProductId = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    LockForm(false);
                    ClearForm();
                    LockForm(true);
                    EnableDisableButtons();
                    LoadProductInfor(_intProductId);
                }
                else
                {
                    txtDescription.Focus();
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void cboFormatCodeID_SelectedValueChanged(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".cboFormatCodeID_SelectedValueChanged()";
            try
            {
                txtPartNumber.Value = DBNull.Value;
                CurrencyManager cm;
                cm = (CurrencyManager) cboFormatCodeID.BindingContext[cboFormatCodeID.DataSource];


                if (cboFormatCodeID.SelectedIndex < 0 || cboFormatCodeID.SelectedValue.ToString().Trim() == string.Empty)
                {
                    txtPartNumber.ReadOnly = true;
                }
                else
                {
                    //get the current row 
                    var drv = (DataRowView) cm.Current;
                    txtPartNumber.EditMask = drv[ITM_FormatCodeTable.CODE_FLD].ToString();
                    txtPartNumber.ReadOnly = false;
                    txtPartNumber.Focus();
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void btnRouting_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnRouting_Click()";
            try
            {
                if (_intProductId > 0 && chkMakeItem.Checked)
                {
                    var frmRoutingForm = new Routing();
                    frmRoutingForm.ProductID = _intProductId;
                    frmRoutingForm.ShowDialog();
                    LoadProductInfor(_intProductId);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            PCSMessageBox.Show(ErrorCode.NOT_IMPLEMENT, MessageBoxIcon.Information);
        }

        private void ProductItemInfo_Activated(object sender, EventArgs e)
        {
            if (_firstLoad)
            {
                txtCode.Focus();
                _firstLoad = false;
            }
        }

        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4)
            {
                if (btnSearchProductCode.Enabled)
                {
                    btnSearchProductCode_Click(null, null);
                }
            }
        }

        private void txtDescription_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4)
            {
                if (btnSearchProductDescription.Enabled)
                {
                    btnSearchProductDescription_Click(null, null);
                }
            }
        }

        private void txtVAT_TextChanged(object sender, EventArgs e)
        {
        }

        private void dtmSetupDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                tabProductInfo.TabIndex = 1;
            }
        }

        private void txtLTRequisition_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                tabProductInfo.TabIndex = 1;
            }
        }

        private void txtSpecialTax_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                tabProductInfo.TabIndex = 1;
            }
        }

        private void chkMakeItem_CheckedChanged(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ". chkMakeItem_CheckedChanged()";
            try
            {
                if (chkMakeItem.Checked)
                {
                    radPlanTypeMPS.Checked = true;
                }
                else
                {
                    radPlanTypeMRP.Checked = true;
                }
            }
            catch (Exception ex)
            {
                // displays the error message.

                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);

                // log message.

                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }

                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnPrint_Click()";
            //return immediatly if none SO selected
            if (_intProductId <= 0) return;

            try
            {
                Cursor = Cursors.WaitCursor;

                // HACKED: thachnn: 01/03/2006: add Item Information Report
                ShowItemInformationReport();
                // ENDHACKED: thachnn: 01/03/2006: add Item Information Report

                Cursor = Cursors.Default;
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// Select inventor information
        /// </summary>
        /// <param name="pstrMethodName"></param>
        /// <param name="pblnAlwaysShowDialog"></param>
        /// <param name="pblnFindById"></param>
        /// <returns></returns>
        /// <author> Tuan TQ</author>
        private bool SelectInventor(string pstrMethodName, bool pblnAlwaysShowDialog)
        {
            try
            {
                var htbCriteria = new Hashtable();
                DataRowView drwResult = null;
                //Call OpenSearchForm for selecting Master Location
                drwResult = FormControlComponents.OpenSearchForm(MST_PartyTable.TABLE_NAME, MST_PartyTable.CODE_FLD,
                                                                 txtInventor.Text, htbCriteria, pblnAlwaysShowDialog);

                // If has Party matched searching condition, fill values to form's controls
                if (drwResult != null)
                {
                    txtInventor.Text = drwResult[MST_PartyTable.CODE_FLD].ToString();
                    txtInventor.Tag = drwResult[MST_PartyTable.PARTYID_FLD];

                    //Reset modify status
                    txtInventor.Modified = false;
                }
                else if (!pblnAlwaysShowDialog)
                {
                    tabProductInfo.SelectedIndex = 0;
                    txtInventor.Focus();
                    return false;
                }

                return true;
            }
            catch (PCSException ex)
            {
                throw new PCSException(ex.mCode, pstrMethodName, ex);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Fill related data on controls when select Currency
        /// </summary>
        /// <param name="pstrMethodName"></param>
        /// <param name="pblnFindByID"></param>
        /// <param name="pblnAlwaysShowDialog"></param>
        /// <returns></returns>
        /// <author> Tuan TQ</author>
        private bool SelectCurrency(string pstrMethodName, bool pblnAlwaysShowDialog)
        {
            try
            {
                var htbCriteria = new Hashtable();
                DataRowView drwResult = null;

                //Call OpenSearchForm for selecting Currency
                drwResult = FormControlComponents.OpenSearchForm(MST_CurrencyTable.TABLE_NAME,
                                                                 MST_CurrencyTable.CODE_FLD, txtCurrency.Text,
                                                                 htbCriteria, pblnAlwaysShowDialog);

                // If has currency matched searching condition, fill values to form's controls
                if (drwResult != null)
                {
                    //Check if master location was changed then clear grid content
                    txtCurrency.Text = drwResult[MST_CurrencyTable.CODE_FLD].ToString();
                    txtCurrency.Tag = drwResult[MST_CurrencyTable.CURRENCYID_FLD];

                    //Reset modify status
                    txtCurrency.Modified = false;
                }
                else if (!pblnAlwaysShowDialog)
                {
                    tabProductInfo.SelectedIndex = 1;
                    txtCurrency.Focus();
                    return false;
                }

                return true;
            }
            catch (PCSException ex)
            {
                throw new PCSException(ex.mCode, pstrMethodName, ex);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        private void btnInventor_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnInventor_Click()";
            try
            {
                SelectInventor(METHOD_NAME, true);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void OnEnterControl(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ". OnEnterControl()";
            try
            {
                FormControlComponents.OnEnterControl(sender, e);
            }
            catch (Exception ex)
            {
                // displays the error message.

                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void cboBuyingUMID_TextChanged(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".cboBuyingUMID_TextChanged()";
            try
            {
                lblBuyingUM1.Text = "(" + cboBuyingUMID.Text + ")";
                lblBuyingUM2.Text = lblBuyingUM1.Text;
                lblBuyingUM3.Text = lblBuyingUM1.Text;
            }
            catch (Exception ex)
            {
                // displays the error message.

                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void OnLeaveControl(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".OnLeaveControl()";
            try
            {
                FormControlComponents.OnLeaveControl(sender, e);
            }
            catch (Exception ex)
            {
                // displays the error message.

                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        // HACK : Tuan DM 17 - 10 - 2005
        private void txtPrimaryVendor_Validating(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".txtPrimaryVendor_Validating()";
            const string VENDOR = "Vendor";
            try
            {
                FormControlComponents.OnLeaveControl(sender, e);
                if (!btnSearchVendor.Enabled || !txtPrimaryVendor.Modified)
                {
                    return;
                }
                if (txtPrimaryVendor.Text.Trim() == string.Empty)
                {
                    txtPrimaryVendorID.Text = string.Empty;
                    txtVendorName.Text = string.Empty;
                    ChangePrimaryVendorID();
                    return;
                }

                //Search for the Primary Vendor ID
                DataRowView drwResult = null;
                var htbCondition = new Hashtable();

                htbCondition.Add(VENDOR, 1);
                drwResult = FormControlComponents.OpenSearchForm(VCustomerVendor, MST_PartyTable.CODE_FLD,
                                                                 txtPrimaryVendor.Text, htbCondition, false);
                if (drwResult != null)
                {
                    txtPrimaryVendorID.Text = drwResult[MST_PartyTable.PARTYID_FLD].ToString();
                    txtPrimaryVendor.Text = drwResult[MST_PartyTable.CODE_FLD].ToString();
                    txtVendorName.Text = drwResult[MST_PartyTable.NAME_FLD].ToString();
                    ChangePrimaryVendorID();
                }
                else
                {
                    e.Cancel = true;
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        // END : Tuan DM 17 - 10 - 2005

        private void btnChangePicture_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnChangePicture_Click()";
            try
            {
                if (DialogResult.Cancel == dlgSelectPicture.ShowDialog())
                    return;
                Image img = Image.FromFile(dlgSelectPicture.FileName);
                _strPathImage = dlgSelectPicture.FileName;
                if (img.Size.Width > picCategory.Size.Width ||
                    img.Size.Height > picCategory.Size.Height)
                    picCategory.SizeMode = PictureBoxSizeMode.StretchImage;
                else
                    picCategory.SizeMode = PictureBoxSizeMode.CenterImage;
                picCategory.Image = Image.FromFile(dlgSelectPicture.FileName);
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnClearPicture_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnClearPicture_Click()";
            try
            {
                picCategory.Image = null;
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnTakingCode_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnTakingCode_Click()";
            try
            {
                DataRowView drwResult = null;
                var htbConditon = new Hashtable();
                if (cboCCN.SelectedValue != null)
                {
                    htbConditon.Add(ITM_ProductTable.CCNID_FLD, cboCCN.SelectedValue.ToString());
                }
                else
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_CCN, MessageBoxIcon.Warning);
                    cboCCN.Focus();
                    return;
                }
                drwResult = FormControlComponents.OpenSearchForm(ITM_ProductTable.TABLE_NAME,
                                                                 ITM_ProductTable.STOCKTAKINGCODE_FLD,
                                                                 txtStockTakingCode.Text, htbConditon, true);
                if (drwResult != null)
                {
                    _intProductId = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    LoadProductInfor(_intProductId);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtStockTakingCode_Leave(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".txtStockTakingCode_Leave()";
            //Only use when users use this to search for existing product
            try
            {
                FormControlComponents.OnLeaveControl(sender, e);
                if (!btnTakingCode.Enabled || !txtStockTakingCode.Modified)
                {
                    return;
                }
                if (txtStockTakingCode.Text.Trim() == string.Empty)
                {
                    _intProductId = -1;
                    ClearForm();
                    EnableDisableButtons();
                    return;
                }
                DataRowView drwResult = null;
                var htbConditon = new Hashtable();
                if (cboCCN.SelectedValue != null)
                {
                    htbConditon.Add(ITM_ProductTable.CCNID_FLD, cboCCN.SelectedValue.ToString());
                }
                else
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_CCN, MessageBoxIcon.Warning);
                    cboCCN.Focus();
                    return;
                }
                drwResult = FormControlComponents.OpenSearchForm(ITM_ProductTable.TABLE_NAME,
                                                                 ITM_ProductTable.STOCKTAKINGCODE_FLD,
                                                                 txtStockTakingCode.Text, htbConditon, false);
                if (drwResult != null)
                {
                    _intProductId = int.Parse(drwResult[ITM_ProductTable.PRODUCTID_FLD].ToString());
                    LockForm(false);
                    ClearForm();
                    LockForm(true);
                    EnableDisableButtons();
                    LoadProductInfor(_intProductId);
                }
                else
                    txtStockTakingCode.Focus();
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtStockTakingCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4)
            {
                if (btnTakingCode.Enabled)
                {
                    btnTakingCode_Click(null, null);
                }
            }
        }

        private void btnworkcenter_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnworkcenter_Click()";
            try
            {
                DataRowView drwResult = null;
                var htbConditon = new Hashtable();
                if (cboCCN.SelectedValue != null)
                {
                    htbConditon.Add(ITM_ProductTable.CCNID_FLD, cboCCN.SelectedValue.ToString());
                }
                else
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_CCN, MessageBoxIcon.Warning);
                    cboCCN.Focus();
                    return;
                }
                drwResult = FormControlComponents.OpenSearchForm(MST_WorkCenterTable.TABLE_NAME,
                                                                 MST_WorkCenterTable.CODE_FLD, txtWorkcenter.Text,
                                                                 htbConditon, true);
                if (drwResult != null)
                {
                    lblWorkCenter.Tag = int.Parse(drwResult[MST_WorkCenterTable.WORKCENTERID_FLD].ToString());
                    txtWorkcenter.Tag = int.Parse(drwResult[MST_WorkCenterTable.WORKCENTERID_FLD].ToString());
                    txtWorkcenter.Text = drwResult[MST_WorkCenterTable.CODE_FLD].ToString();
                    // Change Tag part number
                    int intstart = txtCode.Text.LastIndexOf("-");
                    int intlength = txtCode.Text.Trim().Length;

                    if (intstart > 0)
                    {
                        txtCode.Text = txtCode.Text.Substring(0, intstart) + "-" + txtWorkcenter.Text.Trim();
                    }
                    else
                    {
                        txtCode.Text = txtCode.Text.Trim() + "-" + txtWorkcenter.Text.Trim();
                    }
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtWorkcenter_Validating(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".txtWorkcenter_Validating()";
            try
            {
                if (btnworkcenter.Enabled && txtWorkcenter.Text.Trim() != string.Empty)
                {
                    DataRowView drwResult = null;
                    var htbConditon = new Hashtable();
                    if (cboCCN.SelectedValue != null)
                    {
                        htbConditon.Add(ITM_ProductTable.CCNID_FLD, cboCCN.SelectedValue.ToString());
                    }
                    else
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_RGA_CCN, MessageBoxIcon.Warning);
                        cboCCN.Focus();
                        return;
                    }
                    drwResult = FormControlComponents.OpenSearchForm(MST_WorkCenterTable.TABLE_NAME,
                                                                     MST_WorkCenterTable.CODE_FLD, txtWorkcenter.Text,
                                                                     htbConditon, false);
                    if (drwResult != null)
                    {
                        lblWorkCenter.Tag = int.Parse(drwResult[MST_WorkCenterTable.WORKCENTERID_FLD].ToString());
                        txtWorkcenter.Text = drwResult[MST_WorkCenterTable.CODE_FLD].ToString();
                    }
                }
                else
                {
                    lblWorkCenter.Tag = 0;
                    txtWorkcenter.Text = string.Empty;
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void cboDesignUMID_TextChanged(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".cboDesignUMID_TextChanged()";
            try
            {
                lblDesignUMID.Tag = "(" + cboStockUMID.Text + ")";
            }
            catch (Exception ex)
            {
                // displays the error message.

                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void lblCode_Click(object sender, EventArgs e)
        {
        }

        private void btnViewImage_Click(object sender, EventArgs e)
        {
            if (_strPathImage != "" || _mPicture != null)
            {
                var frViewImage = new ViewImage();
                frViewImage.strPathImage = _strPathImage;
                frViewImage.mPicture = _mPicture;
                frViewImage.Show();
            }
            else
            {
                MessageBox.Show("Chưa insert ảnh! Xin vui lòng chọn ảnh bạn muốn xem");
            }
        }

        #region Added by Tuan TQ - 29 May, 2006

        private bool SelectACAdjustment(bool pblnAlwaysShowDialog)
        {
            var htbCriteria = new Hashtable();
            DataRowView drwResult = null;

            //Call OpenSearchForm for selecting AC Adjustment Master Table
            drwResult = FormControlComponents.OpenSearchForm(cst_ACAdjustmentMasterTable.TABLE_NAME,
                                                             cst_ACAdjustmentMasterTable.CODE_FLD, txtACAdjustment.Text,
                                                             htbCriteria, pblnAlwaysShowDialog);

            // If has currency matched searching condition, fill values to form's controls
            if (drwResult != null)
            {
                txtACAdjustment.Text = drwResult[cst_ACAdjustmentMasterTable.CODE_FLD].ToString();
                txtACAdjustment.Tag = drwResult[cst_ACAdjustmentMasterTable.ACADJUSTMENTMASTERID_FLD];

                //Reset modify status
                txtACAdjustment.Modified = false;
            }
            else if (!pblnAlwaysShowDialog)
            {
                tabProductInfo.SelectedIndex = 2;
                txtACAdjustment.Focus();
                return false;
            }

            return true;
        }
        
        private void txtACAdjustment_Validating(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".txtACAdjustment_Validating()";

            try
            {
                //exit if not in add action or empty
                if (_enumAction == EnumAction.Default) return;

                if (txtACAdjustment.Text.Trim().Length == 0)
                {
                    txtACAdjustment.Tag = null;
                    return;
                }
                else if (!txtACAdjustment.Modified)
                {
                    return;
                }

                e.Cancel = !SelectACAdjustment(false);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtACAdjustment_KeyDown(object sender, KeyEventArgs e)
        {
            const string METHOD_NAME = This + ".txtACAdjustment_KeyDown()";
            try
            {
                if ((e.KeyCode == Keys.F4) && (btnACAdjustment.Enabled))
                {
                    SelectACAdjustment(true);
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnACAdjustment_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnACAdjustment_Click()";
            try
            {
                SelectACAdjustment(true);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        #endregion

        #region HACKED by Tuan TQ -- Nov 03, 2005: Change request, add new fields

        private void txtInventor_KeyDown(object sender, KeyEventArgs e)
        {
            const string METHOD_NAME = This + ".txtInventor_KeyDown()";

            try
            {
                if ((e.KeyCode == Keys.F4) && (btnInventor.Enabled))
                {
                    SelectCurrency(METHOD_NAME, true);
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnSearchCurrency_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnSearchCurrency_Click()";
            try
            {
                SelectCurrency(METHOD_NAME, true);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtCurrency_KeyDown(object sender, KeyEventArgs e)
        {
            const string METHOD_NAME = This + ".txtCurrency_KeyDown()";
            try
            {
                if ((e.KeyCode == Keys.F4) && (btnSearchCurrency.Enabled))
                {
                    SelectCurrency(METHOD_NAME, true);
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtCurrency_Validating(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".txtCurrency_Validating()";

            try
            {
                //exit if not in add action or empty
                if (_enumAction == EnumAction.Default) return;

                if (txtCurrency.Text.Trim().Length == 0)
                {
                    txtCurrency.Tag = null;
                    return;
                }
                else if (!txtCurrency.Modified)
                {
                    return;
                }

                e.Cancel = !SelectCurrency(METHOD_NAME, false);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void numPurchasingPrice_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (!numPurchasingPrice.Text.Trim().Equals(string.Empty))
                {
                    lblCurrency.ForeColor = Color.Maroon;
                }
                else
                {
                    lblCurrency.ResetForeColor();
                }
            }
            catch
            {
            }
        }

        private void txtInventor_Validating(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".txtCurrency_Validating()";

            try
            {
                //exit if not in add action or empty
                if (_enumAction == EnumAction.Default) return;

                if (txtInventor.Text.Trim().Length == 0)
                {
                    txtInventor.Tag = null;
                    return;
                }
                else if (!txtInventor.Modified)
                {
                    return;
                }

                e.Cancel = !SelectInventor(METHOD_NAME, false);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        #endregion HACKED by Tuan TQ -- Nov 03, 2005: Change request, add new fields

        #region Thachnn: Item Information Report relate

        private const string TABLE_NAME = "Table";
        private const string REPORT_NAME = "ItemInformationReport";
        private const string MAIN_META = "ItemInformation";
        private const string STOCKSTATUS = "StockStatus";
        private const string BOM = "BOM";
        private const string ROUTING = "Routing";
        private const string STANDARDCOST = "StandardCost";

        /// Report layout file constant
        private const string REPORT_LAYOUT_FILE = "ItemInformation.xml";

        private const string SUBREPORT_NAME = "SubReport";

        /// <summary>
        /// Show the Item Information report
        /// This report uses the : ItemInformation.xml template
        /// PROGRESS IS:
        // ## 1 ## Get the meta information (single value)
        // ## 2 ## Get the data collection information (data source for sub report StockStatus,BOM,Routing,StandardCost)
        // ## 3 ## Draw actual value to the report field			
        // ## 4 ## Refresh, render report
        // ## 5 ## Show report			
        /// <author>thachnn: 01 03 2006</author>
        /// </summary>
        private void ShowItemInformationReport()
        {
            #region My variables

            const string METHOD_NAME = ".ShowItemInformationReport()";

            string mstrReportDefinitionFolder = Application.StartupPath + "\\" +
                                                Constants.REPORT_DEFINITION_STORE_LOCATION;

            #endregion

            #region	## 1 ## Get the meta information (single value)

            var objProductItemInfoBO = new ProductItemInfoBO();
            DataSet dstResultGetFromDatabase = objProductItemInfoBO.GetItemInformationData(_intProductId,
                                                                                           MAIN_META + TABLE_NAME,
                                                                                           STOCKSTATUS + TABLE_NAME,
                                                                                           BOM + TABLE_NAME,
                                                                                           ROUTING + TABLE_NAME,
                                                                                           STANDARDCOST + TABLE_NAME);

            DataTable dtbMainMeta = dstResultGetFromDatabase.Tables[MAIN_META + TABLE_NAME];
            DataTable dtbStockStatus = dstResultGetFromDatabase.Tables[STOCKSTATUS + TABLE_NAME];
            DataTable dtbBOM = dstResultGetFromDatabase.Tables[BOM + TABLE_NAME];
            DataTable dtbRouting = dstResultGetFromDatabase.Tables[ROUTING + TABLE_NAME];
            DataTable dtbStandardCost = dstResultGetFromDatabase.Tables[STANDARDCOST + TABLE_NAME];

            #endregion

            #region ## 4 ## Refresh, render report

            var subReportBuilder = new ReportWithSubReportBuilder
                                       {ReportName = REPORT_NAME, SourceDataTable = dtbMainMeta};
            subReportBuilder.SubReportDataSources.Add(STOCKSTATUS + SUBREPORT_NAME, dtbStockStatus);
            subReportBuilder.SubReportDataSources.Add(BOM + SUBREPORT_NAME, dtbBOM);
            subReportBuilder.SubReportDataSources.Add(ROUTING + SUBREPORT_NAME, dtbRouting);
            subReportBuilder.SubReportDataSources.Add(STANDARDCOST + SUBREPORT_NAME, dtbStandardCost);

            #region INIT REPORT BUIDER OBJECT

            subReportBuilder.ReportDefinitionFolder = mstrReportDefinitionFolder;
            subReportBuilder.ReportLayoutFile = REPORT_LAYOUT_FILE;
            if (subReportBuilder.AnalyseLayoutFile() == false)
            {
                throw new PCSException(ErrorCode.MESSAGE_REPORT_TEMPLATE_FILE_NOT_FOUND, This + METHOD_NAME,
                                       new FileNotFoundException(This + METHOD_NAME, subReportBuilder.ReportLayoutFile));
            }
            subReportBuilder.UseLayoutFile = true; // always use layout file

            #endregion

            subReportBuilder.MakeDataTableForRender();

            // and show it in preview dialog				
            var printPreview = new C1PrintPreviewDialog {FormTitle = REPORT_NAME};
            subReportBuilder.ReportViewer = printPreview.ReportViewer;
            subReportBuilder.RenderReport();

            #endregion ## 4 ## Refresh, render report

            #region ## 5 ## Show report

            printPreview.FormTitle = subReportBuilder.GetFieldByName("fldTitle").Text;
            printPreview.Show();

            #endregion ## 5 ## Show report
        }

        #endregion Thachnn: Item Information Report relate
    }
}